Repository navigation
Conversation
TicketStore.prune/delete and WebFilterStore.upsert_events/delete_agent open BEGIN IMMEDIATE on their store's connection under write_lock, but the single-statement writers on the same connections (TicketStore.create, update, save_run, the approval/channel/pending-request writes, and WebFilterStore's domain and schedule-window edits) ran their DML and commit() unlocked. Under the legacy isolation level the DML opens an implicit transaction that stays open across the commit's await, so a locked writer interleaving there failed with "cannot start a transaction within a transaction" (periodic prune, ticket delete, telemetry ingest), or committed or rolled back the other writer's half-finished transaction (consume_pending_request's rollback). Each of these writers now takes write_lock around its statements and commit, as create's docstring already claimed. Found by crash fuzzing of telemetry ingest: AlertEngine's periodic TicketStore.prune raced ticket creation. Regression tests run the unlocked writers concurrently with the locked transactions and fail without the fix. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01QcqxZQbDG4F4x9duUyvsxH
This branch has not been deployed
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 & why
Scheduled crash-fuzzing run. Earlier runs covered the pure parsers and request handlers well. This one drove a fake agent through the live telemetry path (
AgentTunnel._serve, then alerting and ticket hooks), and surfaced a race in two SQLite stores.Problem
TicketStore.prune/deleteandWebFilterStore.upsert_events/delete_agentopenBEGIN IMMEDIATEon their store's connection underwrite_lock. The single-statement writers that share those connections run their DML andcommit()without the lock:TicketStore:create,update,save_run,create_approval,decide_approval,set_approval_message,bind_channel,archive_channel,open_pending_request,consume_pending_requestWebFilterStore:add_domain,remove_domain,add_window,set_window_enabled,remove_windowUnder the legacy isolation level, a DML statement opens an implicit transaction. That transaction stays open across the
awaitofcommit(). A locked writer that runs in that window then fails:Alternatively, one writer's
commit()orrollback()lands on the other's half-finished transaction.consume_pending_request's unlockedrollback()can discard a concurrent lockedset_state/append_event.The race reproduces every time:
asyncio.gather(store.create(...), store.prune())raised the error on 200 of 200 iterations. In production it hits the periodic prune, ticket deletion and telemetry ingest whenever they overlap ticket creation, an approval decision, or an operator editing web-filter domains or windows.create's docstring already says its INSERT runs "under the same write lock". It did not.Fix
Each of those writers now takes
write_lock()around its statement(s) andcommit(). Follow-up reads (get,get_approval, ...) stay outside the lock, as ADR-0051 requires: hold the lock only around the statements it protects. The lock is re-entrant per task, so nested calls are unaffected. Every other store either already locks all its writes or never opens an explicit transaction.Tests
There are two regression tests in
tests/test_store_pragmas.py, next to the existingwrite_lockseam tests. Each runs the previously unlocked writers concurrently with the lockedBEGIN IMMEDIATEtransactions on one store, and asserts that none raises. Both fail without the fix withOperationalError('cannot start a transaction within a transaction'), and both pass with it.Fuzzing run summary (nothing else new)
health_rules,hardware_metrics,diffs,forecastandrecommend; parsers inwebfilter,availability,rule_hits,steward_notes,protocol.parse_frameand others; ~16k random requests across all 185 HTTP routes; everyGET /apiroute over hostile stored snapshots; live ingest through the tunnel. The other crashes found are already handled by open PRs Survive malformed agent answers: reliability events, refresh snapshots, screenshots #364, Reject wrongly typed user and ticket fields instead of returning 500 (or disabling on "false") #367, Survive binary tunnel frames, bad telemetry timestamps and unbounded fs_read #370, Reject non-finite float days in availability.parse_days #327 and Reject non-string timezone when building a webfilter window #329. Crashes that only occur with input the wire contract already rejects (for example a non-object section) were dropped./proc, journald, smartctl, sudoers, sysfs), plus frame decoding and policy compiling. 5 seeds, no reachable panic. The existing suite also passes: 588 tests. Per the run's rule, no fuzz harness is committed.Checklist
pytest -q(server: 4262 passed, 2 skipped) andcargo test(agent, unchanged: 588 passed)ruff check .(formatting untouched; the touched files were notruff format-clean before this change)Generated by Claude Code