Skip to content

fix(agent): take the write lock up front for read-then-write session writes - #890

Merged
huichen merged 1 commit into
mainfrom
fix/session-db-lock
Oct 1, 2026
Merged

huichen merged 1 commit into
mainfrom
fix/session-db-lock

Conversation

@huichen

@huichen huichen commented Oct 1, 2026

Copy link
Copy Markdown
Collaborator

Symptom

A workspace session (agent.db 体积膨胀排查) rejected every prompt with session persistence is unavailable: database is locked — for 1h45m after the agent's last restart, while its three most recent runs each failed in ~350 ms (i.e. the error was already recorded, not re-detected per attempt). Restarting the agent cleared it.

Root cause

Every live session owns its own Manager → Database → SQLite connection on the same agent.db, so a different connection can commit while one session's transaction is open. Three Manager transactions read before they write and were opened with db.transaction() (BEGIN DEFERRED):

  • append_run_start — reads the existing run markers, then appends the boundary
  • update_session_info_fields — reads the current metadata, then rewrites it
  • create_fork — reads the parent's entries, then inserts the child

In WAL mode, when another connection commits between the read and the first write, SQLite fails the read→write upgrade immediately with SQLITE_BUSY_SNAPSHOT — whose message is exactly database is locked — and neither busy_timeout nor the retry ladder applies. database::begin_immediate exists precisely for this (its doc comment records the same failure seen when the skill registry wrote the file), but it was only wired into the SqliteStore writes; legacy_import's destination check has the same shape.

Who recorded it: the session's startup metadata sync. The agent started at 15:41:54, hydrated the thread, and its set_model → update_info("model") ran at 15:41:56.038 (the exact sessions.updated_at_ms of the session row). That hydration path swallows the error (let _ = self.set_model(..).inspect_err(..), warning only) — but SessionPersistence::record_result had already stored the message in last_error, and the prompt guard reads that first.

Why it never recovered: with no active run, recover_persistence_degraded bails with session has no active run, so the manual recovery path had nothing to key off and the only way out was an agent restart.

Changes

Those four transactions now go through database::begin_immediate: the write lock is taken at BEGIN, so a competing commit makes us wait (5 s busy_timeout + the retry ladder) instead of losing the upgrade instantly.

Test

metadata_update_survives_a_concurrent_commit_on_another_connection holds BEGIN IMMEDIATE on a second connection to the same file and commits ~200 ms later. Without the fix it fails with exactly Error code 5: database is locked; with it, the update waits for the lock and lands.

Checks

cargo fmt -p future-agent --check, cargo clippy -p future-agent --all-targets -- -D warnings, cargo test -p future-agent.

Not in this PR

A metadata write that fails for any other reason still poisons prompting until restart when no run is active (the swallowed-error path above). Worth a separate look — this PR removes the trigger that hit the field, not the poison mechanism.

…writes

Every live session owns its own SQLite connection to the same agent.db, so the
three inline transactions in `Manager` -- the run-boundary append, the metadata
update, and fork materialization -- read before they write on a connection that
another connection can commit against. In WAL mode such a deferred
read-to-write upgrade fails *immediately* with SQLITE_BUSY_SNAPSHOT
("database is locked"), bypassing both `busy_timeout` and the retry ladder that
`database::begin_immediate` exists to provide. That is the hazard already fixed
for the `SqliteStore` writes, still open on these paths.

A field session hit it at startup: the hydration metadata sync
(`set_model` -> `update_info`) recorded "database is locked" in the session's
`last_error` while its caller only logs that failure, so every later prompt was
rejected with "session persistence is unavailable: database is locked" for
hours -- and with no active run, the manual recovery path had nothing to key
off -- until the agent restarted.

Switch those transactions, and the legacy importer with the same
read-then-write shape, to `database::begin_immediate`. The new test holds the
write lock from a second connection and fails with "database is locked" without
the fix.
@huichen
huichen enabled auto-merge (squash) October 1, 2026 09:41
@huichen
huichen merged commit 9591902 into main Oct 1, 2026
12 checks passed
@huichen
huichen deleted the fix/session-db-lock branch October 1, 2026 09:46
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