Skip to content

3.x.x: Keep the pg_restore session out of the fixture pool - #422

Merged
kiwidream merged 2 commits into
3.x.xfrom
restore-session-off-the-fixture-pool
Sep 16, 2026
Merged

kiwidream merged 2 commits into
3.x.xfrom
restore-session-off-the-fixture-pool

Conversation

@kiwidream

@kiwidream kiwidream commented Sep 16, 2026 •

Copy link
Copy Markdown
Member

support::recovery::restore ran the SQL pg_restore produced on a connection
borrowed from the fixture pool and then handed that connection back. The script
is not pure DDL: it opens with session SET statements, and those survive
COMMIT.

#391 fixed the first symptom — pg_restore emits
SELECT pg_catalog.set_config('search_path', '', false), and a pooled
connection carrying an empty search_path broke a later schema-qualified
DELETE whose trigger body resolves unqualified ledger tables. That fix
restores search_path inside the same transaction. The rest of the block was
still returned: statement_timeout, lock_timeout and
idle_in_transaction_session_timeout, all set to 0. sqlx 0.8.6 releases
pooled connections through a spawned task with no reset, so whether a later
statement lands on an affected connection is a scheduling accident.

The restore now runs on a dedicated PgConnection, in one transaction, and that
connection is closed rather than returned. after_release was deliberately
not used: a pool-wide reset hook masks the class of problem instead of removing
this instance, and a second pool would have the same defect as the shared one.

Closes #411

What changed, precisely

  • restore opens PgConnection::connect(&target.url), asserts
    current_schema() is the target schema before doing anything, runs the script
    in one transaction, commits, and closes the connection.
  • The restore result is evaluated before the close result, so a failing close
    cannot hide why the restore failed. When both fail, the restore error is
    returned with the close failure appended to its message, so neither is lost.
    That precedence is now a named helper, restore_outcome, with unit tests over
    all four combinations.
  • v3.0.0 feat: offer found blocks before audit rebuild and landing #391's in-transaction search_path reset is removed in the same commit. On a
    session that is closed and never returns to a pool it is dead code, and its
    comment — "Return this pooled connection to the fixture's schema before
    reusing it" — would have become false. The symptom is not reintroduced; the
    mechanism that caused it is gone. The new test pins search_path alongside the
    timeouts to prove it.

Verification

Toolchain 1.98.1. PostgreSQL 16.15.

  • migration_rollback at default concurrency against a live database:
    8 passed, 0 failed, including
    frozen_2x_backup_restore_reconciles_before_ack_and_exposes_post_ack_loss.

  • cargo clippy --locked -p qbit-prism-server --all-targets -- -D warnings and
    cargo fmt --check: clean.

  • New gated test
    frozen_2x_backup_restore_session_settings_stay_off_the_fixture_pool restores
    into a fixture, waits until every pooled connection is idle, acquires all of
    them and reads each session setting, then repeats the probe after a failed
    restore. Manifest goes 367 → 368; with GITHUB_JOB=prism-native-postgres and
    no database it fails rather than skips.

  • Reverting the fix fails that test, verbatim:
    Error: after restore: pooled lock_timeout is "0", expected "5s": the pg_restore session reached the fixture pool

  • Mutation testing, three mutations, each killed by a named test:

    Mutation Killed by
    Return the restore session to the pool frozen_2x_backup_restore_session_settings_stay_off_the_fixture_pool
    Drop the schema setup on the dedicated connection that test and frozen_2x_backup_restore_reconciles_before_ack_and_exposes_post_ack_loss, with Error: restore session resolves "public", not the target schema
    Return the close error instead of the restore error when both fail restore_outcome_prefers_the_restore_error_when_both_fail

    The third was run twice: it initially survived, and the helper and its unit
    tests were added in response.

How much of a live hazard this actually was

#411 says the leak was inferred, not reproduced, and confirming it was part
of the work. It is worth being precise about what the reproduction shows.

recovery::Database's pool sets no after_connect, so PostgreSQL's own defaults
for the three timeouts are already 0 — identical to what pg_restore sets. The
ledger fixtures that do pin operator timeouts never run a restore, and since
#410 gave each ledger fixture its own database, a leaked session cannot travel
from the recovery fixture to another test file at all. So the failure #411
describes — a test expecting a lock to time out instead hanging to the outer
timeout — was not reachable on this tree.

The reproduction therefore configures the recovery fixture's URL with the same
timeout values Ledger::connect pins, and shows that the restore session's
zeros survive into a later query on that fixture's own pool. That is a faithful
demonstration of the mechanism, inside one fixture, which is the only place it
can now be shown. It is not a demonstration of a failure that was occurring.

The fix is still worth landing on that basis: it removes a session-state escape
that is invisible until a fixture starts pinning a setting, in the area that has
already cost two qualification runs.

Observation, not addressed here

#410 introduced tests/support/ledger_database.rs — one database per ledger
fixture — and moved eight test files onto it. recovery.rs was left on the older
model: Database::open still does CREATE SCHEMA inside the gate's shared
database and connects with options=-csearch_path=<schema>. Two fixture models
now coexist in one test tree, and this is the only file still on the older one.

Moving it is not a substitute for this fix and is not attempted here: a
per-fixture database still hands out pooled connections, so the restore
session's lock_timeout = 0 would still return to that fixture's own pool and
still reach a later query in migration_rollback. The two changes are
orthogonal. The inconsistency is recorded here so it is not lost.

A note for whoever reads the history

54c0f889, the squash commit for #410, lists v3.0.0 test: isolate restore session settings as its third bullet — the subject of 15bafb7f, the
superseded prior implementation this issue names. That change did not land.
#410 does not touch tests/support/recovery.rs; the file's blob is identical at
3b654b5c, at #410's pre-merge head and at 758f46dc. #410's PR body is
explicit and correct about this — it says support/recovery.rs does not appear
in the diff, that the commit is kept as a superseded attempt, and that the
remaining timeouts are worth a separate issue. Only the squash message misleads.
This PR is the change that bullet appears to promise.


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

support::recovery::restore ran the SQL that pg_restore emits on a
connection borrowed from the fixture pool. That SQL opens with session
SET statements that survive COMMIT: PostgreSQL 16 emits
statement_timeout = 0, lock_timeout = 0 and
idle_in_transaction_session_timeout = 0 besides the search_path reset,
and sqlx 0.8.6 returns a pooled connection through a spawned task with
no session reset, so a later pooled query inherited whichever of them
landed on the same connection.

Run the restore on a dedicated PgConnection instead: one transaction,
commit, then close. The restore result is evaluated before the close
result so a failed close cannot hide the restore error. The session is
built from the fixture URL, which pins search_path to the target
schema, and the helper checks current_schema() against the target
before restoring into it.

Drop #391's in-transaction search_path reset. It returned the pooled
session to the fixture schema before reuse; the dedicated session is
closed and never reused, so the mechanism that produced the
intermittent "relation does not exist" failure is gone rather than
patched over. The new test pins search_path along with the timeouts.

The new gated test opens its target with the ledger's operator
timeouts on the URL, because the fixture's plain pool defaults every
setting pg_restore touches to 0 and would not observe the leak. It
waits for sqlx to return every connection and then reads all of them,
so the check does not depend on release scheduling. Before this change
it fails on every run with "pooled lock_timeout is "0", expected "5s"".

Closes #411.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
The pg_restore session helper evaluated the restore result before the
close result so a failed close could not hide why the restore failed,
but nothing tested that order: swapping the two lines left every
migration_rollback test green, because no test injects a failing close
against a live connection.

Move the precedence into a pure helper, restore_outcome, and unit-test
all four combinations directly. When both fail, the restore error is
returned with the close failure appended to its message, so the
operator reads the restore error first and the close failure is not
lost either. The session is still closed on both paths; only which
error is reported is pinned. Swapping the precedence in the helper now
fails restore_outcome_prefers_the_restore_error_when_both_fail.

Follow-up to #411.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-16T15:32:39.410750Z 14b8ca6 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@kiwidream
kiwidream merged commit a828c21 into 3.x.x Sep 16, 2026
20 checks passed
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