Skip to content

fix: a hook no longer loses its events when the spool lock is busy - #340

Merged
varun369 merged 1 commit into
mainfrom
fix/hook-spool-lock-budget
Nov 20, 2026
Merged

varun369 merged 1 commit into
mainfrom
fix/hook-spool-lock-budget

Conversation

@varun369

Copy link
Copy Markdown
Member

What was wrong

test_concurrent_hook_processes_never_tear_a_line failed now and then on the macOS release gate, and on a loaded laptop. Writer processes raised TimeoutError("hook spool lock is busy"). That is a real product defect, not a test problem. Whenever a producer waits longer than its lock budget, the spool raises, deliver() returns spool_failed, and the events are lost. Nothing counts them in slm doctor.

The lock is held for milliseconds, not microseconds: a directory scan, an open and a write, or a rename. Six writers in a tight loop plus a claim every 50 ms keep it close to saturated. Waiters poll every 2 ms, so the slowest waits run past 1 s. Measured locally with 6 writers: the median wait was 13 ms and the slowest about 285 ms. With 12 writers the slowest wait was 955 ms and one event was lost. The daemon's own claim pass gave up too, which explains the separate "1 error" seen locally.

Fix

  • Writing beside the lock. If a producer cannot get the lock in time, it writes its batch to a file of its own: .<profile>.<ns>-<pid>-<rand>.part, renamed to <profile>.<ns>-<pid>-<rand>.draining. The daemon drains it like any claimed file. Because of the rename, the daemon sees the whole file or nothing.
  • Hook wait budget. Hooks now wait 50 ms for the lock (the rest of their 200 ms cap) instead of 1 s. The daemon's intake keeps 1 s.
  • Cap and counts. The size cap still applies on the new path, and refused events are still counted under the lock. If even that stays busy, the error reaches the caller and is reported. It is never a silent success.
  • Erase and cleanup. GDPR erase removes these files and any .part files. The claim pass deletes .part files older than an hour, which can only come from a producer that died mid-write.
  • Stress test. It now matches the product: half the writers use the hook's wait and half the daemon's. A claim that finds the lock busy is retried on the next pass, as the daemon's writer loop does.

Checks against hiding a lost event

Each check is a test, and each test fails when its safeguard is removed:

  • The file reaches the database. HookEventWriter.drain_once inserts it and the spool ends empty.
  • A half-written file is never visible to a claim or a *.draining/*.jsonl scan.
  • Two producers in the same nanosecond never overwrite each other.
  • Erase removes the files, and the cap still holds with drops counted.
  • A failed write raises and leaves no file behind.
  • Stale .part files are swept and fresh ones are kept.

Test plan

  • New tests fail on main (TimeoutError, or spool_failed from deliver) and pass with the fix.
  • With only the fallback reverted, 8 tests fail, the stress test among them.
  • Six separate mutations, one per safeguard, each fail their test.
  • The stress test was run 30 times under extra CPU load: 19/30 passed on main, 30/30 with the fix.
  • tests/test_hooks plus the spool callers (writer, route, doctor, usage join, secret screen): 647 passed. The two native-hook deadline tests missed their deadline once under the extra load used for the loop, and pass on both trees when run again without it.

🤖 Generated with Claude Code

When many hook processes and the daemon's claim pass contend for the spool
lock, a producer that waited past its lock budget raised TimeoutError and the
hook reported spool_failed: the events were gone and `slm doctor` never
counted them. A producer that cannot get the lock now writes its batch to a
file of its own (written as .part, then renamed to .draining, so the daemon
drains all of it or none), which the daemon drains like any claimed file.

Hooks wait 50 ms for the lock (the rest of their 200 ms cap) instead of 1 s.
The size cap still applies and refused events are still counted. GDPR erase
removes these files, and the claim pass sweeps .part files left by a producer
that died mid-write.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@varun369
varun369 merged commit f033487 into main Nov 20, 2026
7 checks passed
@varun369
varun369 deleted the fix/hook-spool-lock-budget branch November 20, 2026 14:57
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