Skip to content

Backport "reader check" changes from Damus iOS#128

Merged
jb55 merged 1 commit into
damus-io:masterfrom
danieldaquino:gh-127
May 8, 2026
Merged

Backport "reader check" changes from Damus iOS#128
jb55 merged 1 commit into
damus-io:masterfrom
danieldaquino:gh-127

Conversation

@danieldaquino

@danieldaquino danieldaquino commented May 8, 2026

Copy link
Copy Markdown
Contributor

This backports the following commit from the Damus iOS repo: damus-io/damus@fe482a9

The rationale of this change is to reduce the chance of stale reader slots by following this recommendation from lmdb.h docs:

 *	@section caveats_sec Caveats
 *	Troubleshooting the lock file, plus semaphores on BSD systems:
 *
 *	- A broken lockfile can cause sync issues.
 *	  Stale reader transactions left behind by an aborted program
 *	  cause further writes to grow the database quickly, and
 *	  stale locks can block further operation.
 *
 *	  Fix: Check for stale readers periodically, using the
 *	  #mdb_reader_check function or the \ref mdb_stat_1 "mdb_stat" tool.
 *	  Stale writers will be cleared automatically on some systems:
 *	  - Windows - automatic
 *	  - Linux, systems using POSIX mutexes with Robust option - automatic
 *	  - not on BSD, systems using POSIX semaphores.
 *	  Otherwise just make all programs using the database close it;
 *	  the lockfile is always reset on first open of the environment.

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced database initialization process with a new startup check that detects and clears stale connection slots, improving startup reliability and preventing potential issues from residual database locks.

Changelog-Changed: Clear stale LMDB readers on NostrDB init to prevent
reader slot leakage and runaway storage usage

Signed-off-by: Daniel D'Aquino <daniel@daquino.me>
@danieldaquino

Copy link
Copy Markdown
Contributor Author

CC @jb55, please let me know if you have any concerns. Thanks!

@coderabbitai

coderabbitai Bot commented May 8, 2026

Copy link
Copy Markdown

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: faaae869-0817-4fef-b692-53ecb18c8dbe

📥 Commits

Reviewing files that changed from the base of the PR and between 2d459e8 and a15402e.

📒 Files selected for processing (1)
  • src/nostrdb.c

📝 Walkthrough

Walkthrough

This PR adds startup-time detection and cleanup of stale LMDB reader slots. A new helper function (ndb_lmdb_reader_check) wraps the LMDB library's reader-check facility, then integrates it into environment initialization. If reader-slot cleanup fails during startup, initialization aborts and the environment is cleaned up.

Changes

LMDB Reader Slot Initialization

Layer / File(s) Summary
Reader Check Helper Function
src/nostrdb.c
Introduces ndb_lmdb_reader_check(MDB_env *env) that calls mdb_reader_check, logs the result or errors, and returns success/failure.
Integration into Initialization
src/nostrdb.c
After mdb_env_open succeeds in ndb_init_lmdb, the code invokes the reader-slot check; on failure, closes the environment and aborts initialization.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Poem

🐰 A startup dance with slots so stale,
We check and clean when readers fail,
With LMDB's gentle care,
No ghost readers linger there! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: backporting a reader check feature from Damus iOS to nostrdb, which is the primary objective of the PR.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@jb55 jb55 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! lgtm. strfry also has this, i didn't realize how important it was.

@jb55 jb55 merged commit f4591db into damus-io:master May 8, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants