Repository navigation
fix(persistence): finish connection cleanup before a cancellation propagates - #1782
Conversation
…pagates Repeatedly cancelling a transaction could keep its pool slot checked out. Two driver-level gaps combine. SQLAlchemy can drive both a graceful close and a stop on the same aiosqlite connection, whose stop is not idempotent and enqueues a second request behind a worker that already exited. And a statement cancelled mid-await leaves its native cursor held by the exception stack, so the rollback that follows fails with "Connection closed" and the slot is never returned. Close the cursor tracked for the connection before retiring it, make the driver stop idempotent per connection, interrupt the statement SQLite is still running, and finish the whole exit inside a managed task so a repeated cancellation cannot strand the cleanup. Only the transaction that owns the connection runs it, so a nested exit cannot close its caller's cursors. A clean exit stays in the caller's task, which the seekdb shutdown ordering depends on. Without the cursor step the new regression fails: the only pool slot is still checked out after the tenth cancelled transaction. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
|
CI is green on the first attempt (23/23). Worth noting: re-running the same |
…mitting part of it Interrupting a SQLite statement rolls the whole native transaction back, so writes that already reported success are gone while SQLAlchemy still trusts the transaction. Keeping `is_disconnect = False` avoided discarding the connection -- an in-memory StaticPool holds its entire database in it -- but it also let a caller that caught the error keep writing and commit a partial result: insert row 3, interrupt a long UPDATE, insert row 4, and only row 4 survives. Record the interruption on the connection and refuse to commit when the transaction exits, raising PendingRollbackError the way the unmodified baseline does. The connection still returns to the pool, so an in-memory database survives. * `test_interrupted_write_cannot_commit_earlier_work[file|memory]` fails without the guard. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ransaction The mark lives on the pooled connection, and the usage recorder runs its own transaction without going through `AsyncDatabase.transaction()`, so an interrupt recorded there was never consumed. It survived on the connection until an unrelated transaction picked that connection up and raised PendingRollbackError for a rollback it had nothing to do with -- which is how this first showed up, as a topic-memory supervisor test failing on 3.11. Clear any leftover mark when a transaction starts, so only an interrupt raised inside it can fail it. * `test_interrupt_marked_outside_the_transaction_does_not_fail_it` fails without the cleanup. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ection Two follow-ups from review. `connection.info` re-resolves the DBAPI connection, so once a cancelled statement invalidated it through SQLAlchemy's normal path, reading the interruption mark from the failure path raised PendingRollbackError and skipped the rest of `_finish_transaction()`. That left the transaction counted as active and kept `database.close()` from finishing. Hold the info dict itself instead; it stays usable after the connection is gone. The mark was also only checked before COMMIT ran. A commit cancelled while in flight records the mark afterwards, so it survived on the pooled connection and failed the next borrower's transaction. Clear it in a `finally` around the exit, which covers cancellation during COMMIT or ROLLBACK too. * `test_invalidated_connection_still_reaches_transaction_cleanup` fails without the first part. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The earlier fixes addressed review, but two of its requirements were only verified indirectly. Preserving the original cancellation: the invalidation test asserted that cleanup ran, not that the caller still sees CancelledError. Cover that directly. Clearing the mark on a commit that is cancelled: the stale-mark test planted the mark before the transaction started. Plant it from the commit event instead, which is where an error handler records it when the commit itself is interrupted -- after the transaction's own check. That fails without the cleanup. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
aee8478 to
8998b91
Compare
… receives `plant_mark` was annotated `object` and then read `.info` off it, which `ty` rejects: `Object of type object has no attribute info`. Name the SQLAlchemy connection type the commit event actually passes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Preserve the transaction cleanup and atomicity fixes while integrating upstream usage handling. Synchronize request cancellation at the completion flush and retain the storage-failure and recovery checks. Limit the commit-lock regression's short write budget to the deliberately blocked write. Verify recovery with the same recorder's normal budget without changing production timeout or retry behavior. Validated source tree: 1e24ec7 Linux Python 3.14.4: 3486 unit tests and 379 E2E tests passed. Python 3.12.3: 70 related tests passed; all static checks passed. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Integrate the family-scoped search work (oceanbase#1862), which adds an optional consistent_snapshot to AsyncDatabase.transaction. The snapshot setup moves into the split-out _transaction so the cancellation cleanup in transaction() still owns the connection lifecycle unchanged. Resolve the database.py conflict by keeping both: nested lookups gain the upstream snapshot branch, while the outer transaction retains cursor retirement, interruption-atomicity and commit-boundary cleanup. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Bring in the database-migration prototype (oceanbase#1771/oceanbase#1772), loopback proxy bypass (oceanbase#1888), topic-memory window fixes (oceanbase#1842/oceanbase#1898) and related work. Resolve three conflicts: - database.py: keep the split transaction/_transaction so cancellation cleanup still owns the connection; adopt the upstream nested-snapshot comment. - test_sqlite_profile.py: keep this branch's SyncConnection/event imports and its invalidation + commit-mark regression tests. - test_topic_memory_generic_api.py: adopt upstream's reworked cancellation e2e (completion-flush rendezvous with busy_timeout_ms=100), which retains the cancellation, normal/storage-failure, settlement and recovery-usage assertions through the public flush boundary. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The windows job failure is unrelated to this PR's persistence changes. The log shows the real failure is a frontend test in desktop/ui/tests/memory.test.tsx — late save after a page switch preserves the new draft's navigation guard fails with TypeError: finish is not a function at line 370. That aborts pnpm before tauri build runs, so no NSIS bundle is produced and upload-artifact then reports the missing *-setup.exe. finish is only assigned inside the async mockImplementationOnce callback (line 339), and the act() block at line 369–371 can call it before that assignment runs — so this looks like a timing flake in the test from #1663 (the same job is green on master). A likely fix direction is to wait until finish is actually assigned before resolving it — e.g. poll with vi.waitFor(() => { if (typeof finish !== "function") throw new Error("not assigned"); }) right before the act() call. I haven't verified this locally, so treat it as a lead rather than a tested patch. Could a maintainer re-run the failed job? I don't have write access to trigger a re-run myself. |
Closes #1780
Summary
Ensure that cancelling a SQLite transaction does not strand its connection or pool slot, and preserve transaction atomicity when SQLite interrupts an in-flight write.
This also integrates upstream changes without dropping the cancellation, storage-failure or recovery regression coverage.
Changes
Cancellation-safe transaction cleanup
aiosqlite0.22.1'sstop()idempotent per connection, with independently shielded waiters. The shared driver class is not patched.Atomicity and connection reuse
Regression tests and upstream integration
f28f8edf, including completion-flush synchronization (fix(dsh): preserve unrelated user patches during setup #1799), the usage observation-window correction (fix(e2e): give the usage-record observation window the recorder's budget #1846) and bounded attempt retries (Fix/model usage attempt expiry #1838).<0.5sassertion and connection-restoration checks remain; production timeout and retry rules are unchanged.Verification
Validated commit:
1140bd71d79ffe6de8cdf4d91f35e9f23ea4bdff.The committed source tree is identical to the locally validated tree. All 2044 tracked files matched before and after the final Linux runs.
Additional controls:
OperationalError: interrupted.Earlier failed runs were retained. Missing Git/jq/build metadata and loopback proxy interference were corrected only in the isolated verification environment, followed by successful full-suite runs. No failing tests were removed, and no global proxy settings were changed.
Validation limits