Repository navigation
forge_db::begin: a caller cancelled while queued for a connection leaves the queue - #911
Merged
Merged
Conversation
… BEGIN The task that made beginning a transaction safe against cancellation (#570) ran the whole `pool.begin()`, the acquire included. A caller dropped while it waited for a connection of a saturated pool therefore stayed in the pool's queue: the task later took a connection and ran a `BEGIN` nobody waited for, ahead of the callers still waiting. The caller now acquires the connection itself (dropping an acquire is safe, and takes the caller out of the queue), and the task runs the `BEGIN` alone, on that connection. Fixes #602 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012e6DJXKdTkPcQXmHkxQUuH
Member
Author
|
The This is not this pull request's: the same test failed the same way this morning in the merge queue's run for #901 (https://github.com/forgepkg/forge/actions/runs/44060834451), without this change. With this change it passes locally, six runs alone and two runs of the whole Generated by Claude Code |
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.
Fixes #602
Root cause
#570 made beginning a transaction safe against cancellation by running
pool.begin()in a spawned task. That task ran the wholebegin, the pool'sacquire()included, so a caller dropped while it waited for a connection of a saturated pool stayed in the pool's (first in, first out) queue: the task later took a connection, ranBEGIN, and the result nobody took rolled it back, ahead of the callers still waiting.Change
forge_db::begin(crates/db/src/pool.rs) acquires the connection in the caller withpool.acquire(): dropping an acquire is safe, and takes the caller out of the queue. Only theBEGINon that connection runs in the spawned task, so a caller dropped during theBEGINstill leaves no connection inside a transaction sqlx does not know of (CI: a killed Nexus import resumed to "completed" with one of its packages missing (nexus_import test failed on main) #570).BEGINon the acquired connection issqlx::Transaction::begin(connection, None), which is what sqlx 0.9.0'sPool::beginitself calls after its acquire (sqlx-core/src/pool/mod.rs). It is#[doc(hidden)]but public, and the only way to an ownedTransaction<'static, Postgres>on a pool's connection:Acquire for &mut PoolConnectiongives a transaction that borrows the connection. The public signature offorge_db::beginis unchanged, and so are its callers.docs/changes/2.2.0/602.fixed.md.Tests
crates/db/tests/begin.rsgainsa_caller_dropped_while_the_pool_is_exhausted_gives_up_its_place: a pool of one connection, held; three callers offorge_db::beginpolled once (each is then in the pool's queue) and dropped; a fourth that stays; the connection is released. The pool'sbefore_acquirehook counts how often the connection is handed out: once, to the caller that stayed. No sleeps: the ordering comes from polling the callers and from the pool's queue.mainthe new test fails: the connection is handed out 4 times (left: 4, right: 1), the three dropped callers first.cargo test -p forge-db --test beginpasses, both tests, five runs in a row. CI: a killed Nexus import resumed to "completed" with one of its packages missing (nexus_import test failed on main) #570's test,a_caller_dropped_mid_begin_leaves_no_transaction_open, is unchanged.cargo test -p forge-db(all suites),cargo clippy -p forge-db --all-targets -- -D warnings,cargo fmt --all --checkandcargo xtask changelog --checkpass.No admin API changes, so no web UI screen.
Generated by Claude Code