Skip to content

Serialise SQLite bootstrap across processes (patch #180) - #141

Merged
Nichol4s merged 1 commit into
argusfrom
patch/sqlite-bootstrap-lock
Nov 28, 2026
Merged

Nichol4s merged 1 commit into
argusfrom
patch/sqlite-bootstrap-lock

Conversation

@Nichol4s

Copy link
Copy Markdown

Patch bytedance#180. Pythia and pyrrhus keep their app database in SQLite and run two uvicorn workers. At the 2026-11-28 promote both of pythia's workers read revision 0058 and upgraded to 0064 at once. One died on table coord_task_grants already exists; uvicorn respawned it and it found head. Upstream's SQLite bootstrap lock is in-process only, so every migration that adds a table repeats this on those two stacks.

Change (deerflow/persistence/bootstrap.py):

  • After the per-engine asyncio.Lock, _sqlite_lock holds an exclusive flock on <db>.bootstrap.lock beside the database. It covers the whole reflect-and-act sequence, including the alembic worker thread, so a second process reads the revision only after the first has written head.
  • It uses a separate lock file, so alembic's own connections never wait on it; that is the deadlock _sqlite_lock's docstring gives for not using BEGIN IMMEDIATE.
  • It polls LOCK_NB every 0.2 s with asyncio.sleep and waits as long as the holder takes, like the Postgres advisory lock. A cancelled waiter holds nothing, and the kernel releases the lock if its holder dies.
  • In-memory databases, file: URIs and platforms without fcntl keep the in-process lock alone, as before.

Tests: tests/test_persistence_bootstrap_sqlite_file_lock.py, 6 cases.

  • The lock file's path rules.
  • A bootstrap waits while another holder has the lock.
  • A cancelled waiter leaves the lock free.
  • Two worker processes upgrade one file from 0001_baseline and both exit 0 at head. With the lock disabled this case failed 3 runs of 3: one worker died with alembic CommandError ("expected to match one row when updating '0001_baseline' to '0002_runs_token_usage'; 0 found").

The bootstrap, concurrency, sqlite-lock and forward-revision suites give 79 passed and 7 skipped (Postgres). Ruff is clean, and check_patches_ledger.py is consistent.

Ledger: PATCHES.md gains bytedance#180 (TOC row and section, Delete-when: the two stacks move to Postgres or one worker, or upstream serialises SQLite bootstrap).

Roll-out: rides the next acropolis pin bump. It matters at the next migration on pythia or pyrrhus.

🤖 Generated with Claude Code

Two uvicorn workers of one Gateway share pythia's SQLite file. At the
2026-11-28 promote both upgraded 0058 to 0064 at once and one died on
"table coord_task_grants already exists". Hold an flock on
<db>.bootstrap.lock beside the database for the whole bootstrap, after
the in-process lock; poll so the event loop stays free. In-memory
databases and platforms without fcntl keep the in-process lock alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Nichol4s
Nichol4s merged commit 586ef15 into argus Nov 28, 2026
25 checks passed
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