Skip to content

fix(db): replace SQLITE_BUSY busy-wait backoff with Atomics.wait sleep (#985) - #986

Open
alove20 wants to merge 1 commit into
mksglu:mainfrom
alove20:fix/985-sqlite-busy-wait-spin
Open

fix(db): replace SQLITE_BUSY busy-wait backoff with Atomics.wait sleep (#985)#986
alove20 wants to merge 1 commit into
mksglu:mainfrom
alove20:fix/985-sqlite-busy-wait-spin

Conversation

@alove20

@alove20 alove20 commented Jul 22, 2026

Copy link
Copy Markdown

Fixes the CPU-spin half of #985.

withRetry (src/db-base.ts) backs off between SQLITE_BUSY retries, but because better-sqlite3 is synchronous it cannot await a timer, so it slept with a while (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-project content/<hash>.db this 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 blocks delay ms between attempts, and a zero delay returns immediately (preserving the [0, 0, 0] test contract).

Adds tests/util/with-retry.test.ts covering 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.

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.
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.

1 participant