Skip to content

Hold write_lock for every ticket and webfilter store write - #381

Open
t11z wants to merge 1 commit into
mainfrom
claude/hopeful-newton-ldctny
Open

t11z wants to merge 1 commit into
mainfrom
claude/hopeful-newton-ldctny

Conversation

@t11z

@t11z t11z commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

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 / delete and WebFilterStore.upsert_events / delete_agent open BEGIN IMMEDIATE on their store's connection under write_lock. The single-statement writers that share those connections run their DML and commit() without the lock:

  • TicketStore: create, update, save_run, create_approval, decide_approval, set_approval_message, bind_channel, archive_channel, open_pending_request, consume_pending_request
  • WebFilterStore: add_domain, remove_domain, add_window, set_window_enabled, remove_window

Under the legacy isolation level, a DML statement opens an implicit transaction. That transaction stays open across the await of commit(). A locked writer that runs in that window then fails:

sqlite3.OperationalError: cannot start a transaction within a transaction
  alerting.py  _maybe_prune -> ticketstore.py prune -> store.py _begin_immediate

Alternatively, one writer's commit() or rollback() lands on the other's half-finished transaction. consume_pending_request's unlocked rollback() can discard a concurrent locked set_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) and commit(). 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 existing write_lock seam tests. Each runs the previously unlocked writers concurrently with the locked BEGIN IMMEDIATE transactions on one store, and asserts that none raises. Both fail without the fix with OperationalError('cannot start a transaction within a transaction'), and both pass with it.

Fuzzing run summary (nothing else new)

Checklist

  • Tests pass: pytest -q (server: 4262 passed, 2 skipped) and cargo test (agent, unchanged: 588 passed)
  • Lint clean: ruff check . (formatting untouched; the touched files were not ruff format-clean before this change)
  • If the wire contract changed: n/a
  • If the change is architectural: n/a (a bug fix within ADR-0051's existing rule)
  • Docs updated where relevant: n/a
  • Commits are signed off (DCO): not signed off; amending the pushed commit was not permitted in this automated run

Generated by Claude Code

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

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

2 participants