fix(anomaly-detector): replace busy-wait nonce-db lock with async wait and stale-lock recovery - #357
Open
Yinklekay wants to merge 2 commits into
Open
Conversation
…t and stale-lock recovery The synchronous mkdir spin in acquireNonceDbLock pegged a core and blocked the event loop for up to the full 5s timeout on every contended persist, stalling anomaly detection and HTTP handling. It also left orphaned .lock directories behind if a process died mid-write, silently halting nonce persistence forever. - acquireNonceDbLock is now async and yields between retries via a setTimeout promise instead of busy-waiting - locks record the owning PID and acquisition timestamp in owner.json; locks older than NONCE_DB_LOCK_MAX_AGE_MS (default 30s) are reclaimed, with an mtime fallback for orphaned locks without owner metadata - persistence failures now surface as a NONCE_DB_PERSIST_FAILURE alert (console + dashboard dispatch + runOnce return) instead of being swallowed by the poll loop's generic catch - new env knobs: NONCE_DB_LOCK_TIMEOUT_MS, NONCE_DB_LOCK_RETRY_MS, NONCE_DB_LOCK_MAX_AGE_MS Closes Vero-protocol#346 Generated with Codebuff 🤖 Co-Authored-By: Codebuff <noreply@codebuff.com>
…ero-protocol#346) Generated with Codebuff 🤖 Co-Authored-By: Codebuff <noreply@codebuff.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📌 Summary
Replaces the busy-wait nonce-db lock with an async wait and adds stale-lock recovery (Issue #346).
Problem:
acquireNonceDbLockretriedfs.mkdirSyncin a synchronous spin loop that pegged a core and blocked the Node event loop for up to the full 5000ms timeout on every contended persist. SincesaveNonces()runs on every poll cycle, contention stalled all anomaly detection and every outstanding HTTP request. There was also no stale-lock recovery: a process dying betweenmkdirSyncandrmdirSyncleft a permanent.lockdirectory, so every subsequentpersistNonceMapthrew after 5s and nonce state silently stopped being persisted while the detector kept reporting normally.Changes:
acquireNonceDbLockis now async and yields between retries via asetTimeoutpromise — no more busy-wait, the event loop stays responsive under contention.owner.jsoncontaining the owning PID and an acquisition timestamp. Locks older thanNONCE_DB_LOCK_MAX_AGE_MS(default 30s) are reclaimed automatically; orphaned locks without owner metadata fall back to the directory mtime.NONCE_DB_PERSIST_FAILURECRITICAL alert (console + Guardian dashboard dispatch + returned fromrunOnce) instead of being swallowed by the poll loop's generic catch.NONCE_DB_LOCK_TIMEOUT_MS(default 5000),NONCE_DB_LOCK_RETRY_MS(default 50),NONCE_DB_LOCK_MAX_AGE_MS(default 30000). Documented inanomaly-detector/.env.example.🔗 Related Issue
Closes #346
🧪 Testing Completed
npm testinanomaly-detector→ 27 passed across 3 suitesnpx tsc --noEmitandnpm run buildare cleanNONCE_DB_PERSIST_FAILUREalert📋 Contributor Checklist
Closes #<number>