Skip to content

test(backup): close three races in the backup wire tests - #985

Merged
robinnsc merged 1 commit into
mainfrom
test/backup-wire-flakes
Nov 14, 2026
Merged

robinnsc merged 1 commit into
mainfrom
test/backup-wire-flakes

Conversation

@robinnsc

@robinnsc robinnsc commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

What

Test-only fixes for three backup wire cells that failed intermittently on main and in the merge queue on 11 and 12 November, plus two cells with the same port exposure. No product code changes.

  • test_a_tampered_data_file_fails_the_restore_before_any_item (tests/test_backup_job.py): the restore job removes the target and then logs its FAILED line, and the cell read the log once as soon as DescribeTable answered ResourceNotFoundException. It now calls _restore_fails, as the two other failing-restore cells do: the helper waits for the target to go (failing if it is ever anything but CREATING), polls the log for the FAILED line for up to 15 s, and checks the reason and "the target was removed" on it. The cell's own GetItem probe is dropped, since a CREATING target answers no read. Failed in run 43242599309.
  • test_a_row_that_lands_during_the_moves_sync_exits_1 (tests/test_backup_job.py, PostgreSQL): the late row was reported only if it committed before the command's read of the other stores' rows, and the cell relied on listing 2,000 junk prefixes taking longer than its release of the held insert. The cell now drops the row of a second, complete backup before the move, so the move's sync adopts it, and a second trigger holds that adoption insert until the late row exists. The adoption is the sync's only backup_index write and comes before the report's read, so the order no longer depends on the runner. After 30 s the trigger fails the insert with its own message. The junk prefixes are gone, and the cell also checks that the report's Adopted section lists exactly that backup. Failed in runs 42937487371, 43005024164 and 43272409754.
  • TestSqlite::test_an_unreachable_s3_endpoint_starts_the_server_unhealthy_and_recovers (tests/test_backup_store_config.py): the "dead" endpoint port and the server's port were both picked with bind(0) and close. When they are equal, the server's S3 client reaches the server's own TLS listener, which answers plain HTTP with a 301, and the store check reports failed validation instead of unreachable, which is the failure in run 43108980074. The CI log does not show the ports, so the collision is inferred there; forcing it locally gives the same reason. The cell now keeps a socket bound to the dead port without listening (connections are refused and nothing else can bind it), never the server's port, and closes it just before the forwarder takes the port. The reason assertion prints the server log on failure.
  • test_a_store_outage_mid_download_leaves_the_restore_running_and_it_completes and the tarpit in TestSqlite::test_a_stalled_s3_endpoint_holds_neither_the_listener_nor_the_reconciler: each bound a port picked before the server bound its own, so a collision made the server exit with "Address already in use". Both now exclude the server's port; _free_port takes the ports to avoid and is _closed_port with the socket closed.

Why

These cells were the only intermittent failures in the backup suites in the last 100 Integration Tests runs (11 Nov 01:09 to 12 Nov 20:01 UTC). The other failures in that window were a PR's own tests (#618's branch), a GitHub artifact-service outage, and compile errors in the merge queue.

Testing done

Against release builds of this branch rebased on main 22ed10b (no Rust change), PostgreSQL 16 and moto on loopback, not as root:

  • python3 -m pytest tests/test_backup_store_config.py tests/test_backup_job.py -v -rP -p no:cacheprovider, as CI runs it: SQLite 50 passed, 15 skipped; PostgreSQL 45 passed, 20 skipped. Before fix(engine): validate numbers on every key and item path #620 replaced a 4-case cell with a 2-case one, the same run on main 8f932f3 gave 52/15 and 47/20, the counts of the CI runs above with the failed cell passing.
  • The first four cells passed in 8 local runs per backend that runs them, and in 5 more by a reviewer. After the review changes every changed cell passed again (three more runs per backend at the final commit), and the full-file counts above are from the final commit. Giving _restore_fails the wrong reason text fails the tampered cell, so the helper still pins the hashing pass's reason.
  • Moves-sync cell with the release delayed by 1, 3, 5 and 10 s: main's cell fails at the landed-row assertion each time, this branch's passes. With the gate trigger removed, this branch's cell fails every time, so the gate is what orders the two. Delayed past 30 s, it fails in 42 s with "landed_gate: the late row did not land within 30 s" in the output.
  • Main's unreachable-S3 cell with the dead port set to the server's port fails with failed validation, CI's message; the store-outage and tarpit cells with the same collision fail with "Address already in use". With this branch neither helper can return the server's port.
  • A bound, non-listening socket refused 200 of 200 connections, the health reason stayed unreachable across the reconciler's repeated checks, and the forwarder bound the port at once after close() 50 of 50 times.
  • None of the changed code runs on MongoDB: the mongodb-backup-store job runs tests/test_backup_job.py without an S3 endpoint, and the three changed cells there skip before the new code.

Not changed here

  • run-postgres-storage-tests in run 43094482407 failed update_table_creates_a_vector_index_and_backfills_what_is_already_there (crates/storage-postgres/tests/vector_control_plane.rs:964, hold count 1, expected 0). The product marks the index ACTIVE in the catalog and then releases the hold in the data database, and the test reads the hold count once after seeing ACTIVE: the same read-before-write shape as the first cell. It is outside the backup suites, so it is left for a separate change.
  • After a cell stops its forwarder, the port is free again until the cell's next step; another listener could take it in that window. The server already holds its own port then, so only an unrelated process could collide. Left as is.

Checklist

  • I have read CONTRIBUTING.md
  • All tests pass (cargo test --workspace): Python test changes only
  • Code is formatted (cargo fmt --check): Python test changes only
  • Clippy is clean (cargo clippy -- -W clippy::pedantic): Python test changes only
  • I have added or updated tests for new functionality
  • I have updated documentation if behavior changed: no behaviour change
  • Breaking changes are noted below (if any)
  • If this changes the wire protocol, Storage trait, auth model, on-disk
    format, or public CLI surface, an RFC has been accepted or is linked
    below. Otherwise, an ADR captures the decision (link below).

ADR / RFC: n/a

Breaking changes

None.


By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache License 2.0 and I agree to the Developer Certificate of
Origin (DCO). See CONTRIBUTING.md for details.

@LeeroyHannigan LeeroyHannigan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I merged this branch with main at 3a5c84b, built the merge for PostgreSQL and SQLite, and ran the two backup files in a container as root, with moto on loopback and PostgreSQL 16.

What I ran

  • CI's command on the PostgreSQL build: 43 pass, 21 skip and 1 fails. The failure is test_sync_is_refused_while_purge_holds_the_claim, which relies on chmod 0o500. Root ignores that mode, and main's file fails the same way when it runs as root.
  • On the SQLite build: 49 pass and 16 skip with moto, and 47 pass and 18 skip without an S3 endpoint.
  • The counts are lower than the description's because #620 replaced a 4-case cell with a 2-case one, and test_a_prefix_the_store_cannot_read_leaves_the_sync_incomplete skips as root.
  • The changed cells passed three more times on each build.
  • Moves-sync cell with the release delayed: at 1 s and 5 s, main's cell fails at the landed-row assertion and this branch's passes. This branch's cell also passes at 3 s and 10 s. With landed_gate removed and a 1 s delay, it fails at the landed-row assertion. With a 35 s delay it fails after 45 s with "landed_gate: the late row did not land within 30 s".
  • Port collisions forced on main give CI's errors: failed validation in the unreachable cell, and "Address already in use" in the store-outage and stalled cells. With the first bind forced onto the server's port, _closed_port and _free_port pick another port and the three cells pass.
  • Each of these changes fails the cell it targets:
    • no dead.close() before the forwarder starts: the forwarder's bind fails;
    • a listening socket from _closed_port: the store stays checking;
    • a gate that never opens: the cell fails after 42.8 s with the gate's message;
    • the adoptee's prefix left out of the copy: the cell fails at the Adopted assertion.
  • Between 11 Nov 01:00 and 12 Nov 20:30 UTC, 14 Integration Tests runs failed: the six the description cites, 2 merge-queue build failures, 3 artifact upload failures and 3 runs of #618's branch.
  • This branch merges with main without conflicts. Of the open PRs, it conflicts only with #983, which fixes the tampered and moves-sync cells another way. I'm closing #983 in favour of this PR.

Open items

  1. Needed before merge: the tampered cell's new log poll repeats _restore_fails. The inline comment on tests/test_backup_job.py:974 has the one-call version.
  2. Minor: _free_port repeats _closed_port's loop (tests/test_backup_store_config.py:117).
  3. Minor: the moves-sync cell's new Adopted assertion can use _adopted_section (tests/test_backup_job.py:3768).
  4. Minor: the stalled cell makes the socket from _closed_port listen, and the helper's docstring says that socket is not listening (tests/test_backup_store_config.py:857).

With the three code changes applied, tests/test_backup_store_config.py and the three changed cells of tests/test_backup_job.py passed on both builds.

Comment thread tests/test_backup_job.py Outdated
Comment on lines +974 to +983
# The job logs its FAILED line after it removes the target, so the
# target can be gone before the line is written: poll for a moment.
deadline = time.monotonic() + 15
while True:
failed = [line for line in server.log_text().splitlines() if ": FAILED (" in line and f" into {target} " in line]
if failed or time.monotonic() > deadline:
break
time.sleep(0.25)
assert len(failed) == 1, server.log_text()[-4000:]
assert "FAILED (data file data/000002.json.gz does not match the manifest" in failed[0], failed[0]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 976-985 are the body of _restore_fails (line 1550) with the needle written out. Lines 953-955 are its first three lines. Only the GetItem loop at 956-973 differs: the helper waits with _wait_table_gone, which fails if the target is ever anything but CREATING. The docstring says at 936-938 that a CREATING target answers no read, so the helper still checks that no item became readable. The two other failing-restore cells (lines 1603 and 1627) call it.

Lines 952-985 can be this call:

        _restore_fails(
            server, ddb, f"{name}-r", arn,
            "FAILED (data file data/000002.json.gz does not match the manifest",
        )

The docstring's sentence about the GetItem probe (936-938) then goes. With this change the cell passed four times on PostgreSQL and twice on SQLite.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 398f1be: the cell now calls _restore_fails with that needle, and the GetItem sentence is gone from the docstring. The helper still pins the reason: with the needle changed to the sha256-mismatch text the cell fails.

Comment thread tests/test_backup_store_config.py Outdated
Comment on lines +117 to +122
while True:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("127.0.0.1", 0))
port = s.getsockname()[1]
if port not in avoid:
return port

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is _closed_port's loop below, except that the socket is closed. _free_port can call it:

Suggested change
while True:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("127.0.0.1", 0))
port = s.getsockname()[1]
if port not in avoid:
return port
s, port = _closed_port(*avoid)
s.close()
return port

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, _free_port is now _closed_port with the socket closed.

Comment thread tests/test_backup_job.py Outdated
assert late is not None, result
assert released, "the move's sync line was printed before it ended"
assert f"Moved (each row now names the configured store):\n {moved}" in text, text[-4000:]
assert "--- Adopted (an AVAILABLE row inserted for each):\n " + adoptee in text, text[-4000:]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other sync cells in this file read this section with _adopted_section (line 2351). Comparing the whole list also fails if the sync adopted more than the adoptee:

Suggested change
assert "--- Adopted (an AVAILABLE row inserted for each):\n " + adoptee in text, text[-4000:]
assert _adopted_section(text).splitlines()[1:-1] == [f" {adoptee}"], text[-4000:]

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, it compares the whole Adopted section with _adopted_section, so an extra adoption fails it too.

Comment thread tests/test_backup_store_config.py Outdated
Comment on lines +857 to +858
# Never the server's port, which the server has not bound yet.
tarpit, tarpit_port = _closed_port(d.port)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_closed_port's docstring says its socket is not listening, and line 859 makes this one listen. The comment could say so:

Suggested change
# Never the server's port, which the server has not bound yet.
tarpit, tarpit_port = _closed_port(d.port)
# Never the server's port, which the server has not bound yet. The
# tarpit listens on the socket that `_closed_port` bound.
tarpit, tarpit_port = _closed_port(d.port)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, the comment now says the tarpit listens on the socket _closed_port bound.

Three backup wire cells failed on main and in the merge queue between
11 and 12 November without a product change. Each depended on timing
or on two independently picked ports differing.

test_a_tampered_data_file_fails_the_restore_before_any_item read the
server log once, as soon as DescribeTable answered
ResourceNotFoundException. The restore job removes the target and only
then logs its FAILED line, so the read could come first (run
43242599309). The cell now calls _restore_fails, which waits for the
target to go (failing if it is ever anything but CREATING), polls the
log for the FAILED line for up to 15 s, and checks the reason and the
target's removal on it. Its own GetItem probe goes: a CREATING target
answers no read.

test_a_row_that_lands_during_the_moves_sync_exits_1 relied on the
move's sync listing 2,000 junk prefixes for longer than the test took
to release the held CreateBackup insert. When the listing won, the
late row committed after the command read the other stores' rows and
went unreported (runs 42937487371, 43005024164, 43272409754). The
cell now drops the row of a second, complete backup before the move,
so the sync adopts it, and a second trigger holds that adoption insert
until the late row exists. The adoption is the sync's only
backup_index write and comes before the closing report's read, so the
late row always commits before that read. After 30 s the trigger fails
the insert with its own message instead. The junk prefixes are
removed, and the cell checks that the report adopted exactly that
backup. With the
release delayed by 1, 3, 5 or 10 s, the old cell fails at the
landed-row assertion and the new one passes.

test_an_unreachable_s3_endpoint_starts_the_server_unhealthy_and_recovers
picked its dead endpoint port with bind(0) and close, before the
server had bound its own port, which was picked the same way. When the
two were equal, the server's S3 client reached the server's own TLS
listener, which answers plain HTTP with a 301, so the store check
reports "failed validation" instead of "unreachable". That is the
failure in run 43108980074. The CI log does not show the ports, so
the collision there is inferred; setting the dead port to the
server's reproduces the same reason.
The cell now keeps a socket bound to the dead port, not listening, so
connections are refused and nothing else can bind it, picks a port
other than the server's, and closes the socket just before the
forwarder takes the port. The reason assertion prints the server log
when it fails.

test_a_store_outage_mid_download_leaves_the_restore_running_and_it_completes
and the tarpit of
test_a_stalled_s3_endpoint_holds_neither_the_listener_nor_the_reconciler
had the same exposure: each bound a port picked before the server had
bound its own, so the server could fail to start with "Address already
in use". Both now exclude the server's port. _free_port takes the
ports to avoid and is _closed_port with the socket closed.

Signed-off-by: Scott Robinson <robinnsc@amazon.com>
@robinnsc
robinnsc force-pushed the test/backup-wire-flakes branch from a621190 to 398f1be Compare November 13, 2026 19:25
@robinnsc

Copy link
Copy Markdown
Collaborator Author

Thanks Lee. I've pushed 398f1be with all four changes, rebased on main 22ed10b. On that base the two files give SQLite 50 passed, 15 skipped and PostgreSQL 45 passed, 20 skipped (not as root), the lower counts being #620's, and the description now says so. The changed cells passed three more times on each build. Thanks for closing #983.

@robinnsc
robinnsc dismissed LeeroyHannigan’s stale review November 13, 2026 23:49

Addressed comments

@robinnsc
robinnsc added this pull request to the merge queue Nov 13, 2026
Merged via the queue into main with commit 95425f5 Nov 14, 2026
51 checks passed
@robinnsc
robinnsc deleted the test/backup-wire-flakes branch November 14, 2026 00:25
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.

3 participants