Skip to content

forge_db::begin: a caller cancelled while queued for a connection leaves the queue - #911

Merged
schmitch merged 1 commit into
mainfrom
claude/602-begin-cancelled-acquire
Nov 17, 2026
Merged

schmitch merged 1 commit into
mainfrom
claude/602-begin-cancelled-acquire

Conversation

@schmitch

Copy link
Copy Markdown
Member

Fixes #602

Root cause

#570 made beginning a transaction safe against cancellation by running pool.begin() in a spawned task. That task ran the whole begin, the pool's acquire() 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, ran BEGIN, 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 with pool.acquire(): dropping an acquire is safe, and takes the caller out of the queue. Only the BEGIN on that connection runs in the spawned task, so a caller dropped during the BEGIN still 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).
  • The BEGIN on the acquired connection is sqlx::Transaction::begin(connection, None), which is what sqlx 0.9.0's Pool::begin itself calls after its acquire (sqlx-core/src/pool/mod.rs). It is #[doc(hidden)] but public, and the only way to an owned Transaction<'static, Postgres> on a pool's connection: Acquire for &mut PoolConnection gives a transaction that borrows the connection. The public signature of forge_db::begin is unchanged, and so are its callers.
  • A changelog fragment, docs/changes/2.2.0/602.fixed.md.

Tests

crates/db/tests/begin.rs gains a_caller_dropped_while_the_pool_is_exhausted_gives_up_its_place: a pool of one connection, held; three callers of forge_db::begin polled once (each is then in the pool's queue) and dropped; a fourth that stays; the connection is released. The pool's before_acquire hook 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.

No admin API changes, so no web UI screen.


Generated by Claude Code

… 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

Copy link
Copy Markdown
Member Author

The test job's first run failed in a_stalled_pull_of_a_downloading_layer_releases_its_staging_space_once_stored (crates/server/tests/registry.rs): the first megabyte of the layer did not arrive within the test's 10 seconds.

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 registry binary. I filed #915 for it and re-ran the failed job once.


Generated by Claude Code

@schmitch
schmitch added this pull request to the merge queue Nov 17, 2026
Merged via the queue into main with commit d24ec79 Nov 17, 2026
33 of 34 checks passed
@schmitch
schmitch deleted the claude/602-begin-cancelled-acquire branch November 17, 2026 17:05
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.

forge_db::begin: a caller cancelled while waiting for a connection still takes one and runs BEGIN for nobody

2 participants