Skip to content

3.x.x: make the probe-window dead-connection test deterministic - #507

Merged
djh58 merged 1 commit into
3.x.xfrom
djh58/prism-b-flaky-probe
Sep 24, 2026
Merged

djh58 merged 1 commit into
3.x.xfrom
djh58/prism-b-flaky-probe

Conversation

@djh58

@djh58 djh58 commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Makes the two idle-gated checkout probe tests added in #499 deterministic. Test-only; the acquire code is untouched and no assertion is weakened.

Failure. ledger::connect::acquire::probe_tests::a_dead_idle_connection_inside_the_probe_window_fails_one_statement_then_recovers failed in PR CI (shard 1, unrelated to that PR's diff) with a statement on the terminated backend 1108 returned pid 1112.

Cause. The test runs one statement on the pool's single connection, then terminates that backend from an admin session and expects the next statement to fail on the dead socket. But sqlx returns a dropped PoolConnection from a task it spawns, and that task pings the server before the connection becomes idle. If the backend is terminated while the release ping is still in flight, the ping fails, sqlx discards the connection immediately, and the checkout under test opens a fresh connection: the statement succeeds on a new pid, which is exactly the message seen. On a two-core host with the runtime and PostgreSQL competing for CPU this reproduced in 21 of 100 runs of the probe tests (two CPU hogs pinned to the same cores, private PostgreSQL 16 with fsync on). The sibling threshold-zero test cannot fail this way, but in the same race it passes for the wrong reason (the connection is discarded at release rather than by the probe).

Fix. A settled helper waits, with a 5 s bound, until the pool reports its one connection idle (num_idle() == 1, size() == 1) before the backend is terminated, and each test asserts the connection is still idle when the checkout starts. The dead socket is then deterministically the one handed out: inside the window the first statement fails on it and the release-time ping discards it; at threshold zero the probe discards it and the statement runs on a fresh connection.

Loop evidence (whole probe_tests module of the server lib binary, pinned to two cores with two CPU hogs and PostgreSQL beside it):

binary runs failures
before 100 21 (returned pid)
after 300 0

The whole server lib test binary (544 tests, PostgreSQL-backed ones included) also passed once on the same two cores without hogs, and that run's gate manifest passes the checker against the binary's 134 gated ids.

Checks. cargo clippy --locked -p qbit-prism-server --all-targets -- -D warnings clean; cargo fmt --all --check clean; the gate-manifest checker passes for the server lib binary's gated ids on a full run of that binary; test/prism-gated-tests.txt unchanged.

Refs #499

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

ledger::connect::acquire::probe_tests::a_dead_idle_connection_inside_the_probe_window_fails_one_statement_then_recovers
failed in CI with "a statement on the terminated backend N returned pid
M". The test terminates the pool's one backend right after a statement,
but sqlx returns a dropped PoolConnection from a spawned task that pings
the server before making the connection idle. When the backend is
terminated while that ping is in flight, the ping fails, sqlx discards
the connection on the spot, and the checkout under test opens a fresh
connection instead of receiving the dead socket, so the statement
succeeds on a new pid. On a two-core host with the runtime and the server
competing for CPU this happened in 21 of 100 runs.

Both probe tests now wait, bounded, until the pool reports the connection
idle before terminating its backend, and assert it is still idle when the
checkout starts, so the dead socket is deterministically the one handed
out. The sibling threshold-zero test could pass for the wrong reason in
the same race (a connection discarded at release, not by the probe); it
gets the same synchronization. No assertion was weakened; the acquire
code is untouched.

Refs #499

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@djh58

djh58 commented Sep 24, 2026

Copy link
Copy Markdown
Member Author

Review ledger (head 791f879, CI green)

Cause. sqlx returns a dropped PoolConnection from a spawned task that pings the server before the connection becomes idle. The test terminated its backend right after a statement; when the termination landed during that release ping, sqlx discarded the connection on the spot and the "dead socket" checkout opened a fresh connection, so the statement succeeded on a new pid (the CI message).

Fix. Test-only: a bounded settled() helper waits until the pool reports its one connection idle before the backend is terminated, and both probe tests assert it is still idle when the checkout starts. No assertion weakened; the acquire code is untouched.

Stress evidence (probe tests, two cores with CPU hogs and PostgreSQL beside them): before 21 failures in 100 runs; after 0 in 300. The full server lib binary also passed on the same cores.

Reviews.

  • Coordinator: reviewed the staged diff (settled() plus the num_idle guards), approved.
  • /code-review medium: no findings; independently confirmed the sqlx release-ping ordering and that num_idle is a dedicated counter incremented only on release.
  • Adversarial (Fable): no refutation found; attacked the settle wait racing another pool user (none exists), a late FIN before the checkout (still an error inside the 5 s bound, never a success), and a release ping failing for another reason (settled() times out with an explicit message instead of a false pass).
  • Thermo-nuclear: one minor note, the post-terminate num_idle == 1 guard is repeated in both tests; kept at the call sites so the precondition reads beside the checkout it guards. Rejected.
  • CI, tenki, codex-on-github: green, no comments.

Refs #499

@djh58
djh58 merged commit 623aa86 into 3.x.x Sep 24, 2026
21 checks passed
djh58 added a commit that referenced this pull request Sep 24, 2026
…-paging

Both sides add metric families. Conflicts resolved by hand in the census
tests only: `tests/observability/cardinality.rs` re-derived from the merged
registry (60 families; 241 startup and 675 populated series: #504's four
families and six series plus this branch's three unlabeled gauges), and
`tests/observability/contract.rs` keeps both sides' gauge names. #502 and
#507 touch no metrics files. The auto-merged registry, metrics, coordinator,
ledger, metadata, inventory and gated-test list were checked: every family
and constant from both sides is present, the metadata names all resolve in
the registry, the inventory is regenerated, and the gated list is byte-sorted
with no duplicates.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
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