fix(db): replace SQLITE_BUSY busy-wait backoff with Atomics.wait sleep (#985) - #986
Open
alove20 wants to merge 1 commit into
Open
fix(db): replace SQLITE_BUSY busy-wait backoff with Atomics.wait sleep (#985)#986alove20 wants to merge 1 commit into
alove20 wants to merge 1 commit into
Conversation
mksglu#985) better-sqlite3 is synchronous, so withRetry cannot await a timer between attempts. The backoff slept with a busy-wait while-loop, pinning a CPU core for the full 100+500+2000ms window. Under multi-session write contention on the shared per-project content DB this accumulated hundreds of CPU-seconds per instance. Use Atomics.wait for a real blocking sleep; a zero delay still returns immediately, preserving the existing test contract. Adds withRetry unit coverage.
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.
Fixes the CPU-spin half of #985.
withRetry(src/db-base.ts) backs off betweenSQLITE_BUSYretries, but because better-sqlite3 is synchronous it cannotawaita timer, so it slept with awhile (Date.now() - start < delay)busy-wait. That pins a CPU core for the full 100+500+2000 ms window; under multi-session write contention on the shared per-projectcontent/<hash>.dbthis accumulated hundreds of CPU-seconds per instance (observed: 99% of a core, 665 lifetime CPU-s).Change: replace the spin with
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, delay)— a real blocking sleep with no CPU cost. Behavior is otherwise identical: it still blocksdelayms between attempts, and a zero delay returns immediately (preserving the[0, 0, 0]test contract).Adds
tests/util/with-retry.test.tscovering retry-then-succeed, non-BUSY rethrow, exhaustion, the source-level guard, and that a non-zero delay elapses.Does not address the underlying contention (shared per-project DB); that's the other half of #985 and a larger change.