Repository navigation
fix: a hook no longer loses its events when the spool lock is busy - #340
Merged
Merged
Conversation
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]>
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.
What was wrong
test_concurrent_hook_processes_never_tear_a_linefailed now and then on the macOS release gate, and on a loaded laptop. Writer processes raisedTimeoutError("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()returnsspool_failed, and the events are lost. Nothing counts them inslm 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
.<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..partfiles. The claim pass deletes.partfiles older than an hour, which can only come from a producer that died mid-write.Checks against hiding a lost event
Each check is a test, and each test fails when its safeguard is removed:
HookEventWriter.drain_onceinserts it and the spool ends empty.*.draining/*.jsonlscan..partfiles are swept and fresh ones are kept.Test plan
main(TimeoutError, orspool_failedfromdeliver) and pass with the fix.main, 30/30 with the fix.tests/test_hooksplus 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