Skip to content

Fix flaky test_shutdown_cancels_backup - #138099

Queued
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:fix-flaky-test-shutdown-cancels-backup
Queued

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:fix-flaky-test-shutdown-cancels-backup

Conversation

@groeneai

@groeneai groeneai commented Dec 6, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #118630

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

...

Description

test_backup_restore_on_cluster/test_cancel_backup.py::test_shutdown_cancels_backup failed 3 times in 90 days with BACKUP_CREATED instead of BACKUP_CANCELLED, always in targeted jobs on unrelated PRs (amd_tsan, amd_binary, arm_binary).

The test checks that the backup is running, then calls restart_clickhouse, which reaches SIGTERM only through docker exec calls. In all three failures the first one (ps -C clickhouse) took 12.9-14.4 s (median 35 ms in the same jobs), queued behind another pytest worker's container operation on the shared docker daemon. The backup of the 10-part table lasts ~14 s, so it had finished when SIGTERM arrived.

Changes (test only):

  • Build the table with 100 parts and sync the replicas before the backup, as test_long_disconnection_stops_backup does. The backup now lasts ~105 s on both hosts and is still cancelled within a second of SIGTERM.
  • Wait for the restarted node's own part of the backup (is_initial_query = 0, as test_cancel_backup does). When the restarted node was the initiator, its ON CLUSTER query satisfied the old check before its part had started. Its DDL entry was then refused during shutdown and replayed after the restart, and the replay briefly re-created the backup's ZooKeeper node, which NoTrashChecker saw in 1 of 10 local runs.

Validation (local, debug build): with a 20 s sleep before the restart standing in for the stall, master fails 3/3 with both CI signatures and this PR passes 3/3. With the initiator forced to be the restarted node: 0 refused or replayed DDL entries in 5 runs (24 refusals without the filter). The module passes, and the test passes 10/10.

The single-node test_backup_restore_new sibling test_shutdown_cancel_backup is unchanged: its backup lasts ~32 s, more than twice the longest stall seen, and it has no such failures.


Workflow [PR]
Sync PR [sync-upstream/pr/138099]

test_shutdown_cancels_backup checks that the backup is in flight and then
calls restart_clickhouse(), which reaches SIGTERM only through docker exec
calls (`ps -C clickhouse`, then `pkill -15`). In three CI failures that `ps`
exec took 12.9-14.4 s (median 35 ms), stalled behind another pytest worker's
container operation on the shared docker daemon. The sleep-dominated backup
of the 10-part table lasts ~14 s, so it finished before SIGTERM arrived and
was reported BACKUP_CREATED instead of BACKUP_CANCELLED.

Build the table with 100 parts and sync the replicas before the backup, so
each replica backs up about half of them: the backup now lasts ~105 s on
both hosts and is still cancelled within a second of SIGTERM.

The in-flight check also counted the initiator's own ON CLUSTER query.
When the initiator was the restarted node, SIGTERM could arrive before its
host-level part had started: its DDL entry was refused during shutdown,
replayed after the restart, and the replay briefly re-created the backup's
ZooKeeper node, which NoTrashChecker could see. Wait for the host-level
part (is_initial_query = 0) instead, as test_cancel_backup does.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118630&sha=5ebb994042de3872804b53a14cc44d972f3e3286&name_0=PR&name_1=Integration%20tests%20%28amd_tsan%2C%20targeted%29
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=gh-readonly-queue/master/pr-116528-e63bd29d291e9bd8413d5f1c07ac7f2cc663d7e4&sha=8d47e250cfd30e1fc1c4a6cf2a3e8bcace0dec59&name_0=MergeQueueCI&name_1=Integration%20tests%20%28amd_binary%2C%20targeted%29
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117814&sha=d9b5c6c2b740e7c6bef2c9b6fc53e41f7cbed2d4&name_0=PR&name_1=Integration%20tests%20%28arm_binary%2C%20distributed%20plan%2C%20targeted%29

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@groeneai

groeneai commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 findings), plus my own cold review (0 findings).

# Sev Finding Verdict Evidence / action
1 ⚠️ Neither change is exercised deterministically: a revert would still pass most CI runs (test_cancel_backup.py:651-675) DISAGREE A flaky-test fix has no CI-resident oracle for itself: reproducing the docker exec stall in the test needs a fixed time.sleep or a stress loop, both ruled out by .claude/CLAUDE.md. The product oracle is intact: if shutdown stopped cancelling, the ~100 s backup would end BACKUP_CREATED and the test would fail. Both directions shown locally with temporary patches: 20 s delay before the restart, master 3/3 FAIL, this PR 3/3 PASS; forced initiator restart, 24 refused and replayed DDL entries without the filter, 0 with it.
2 💡 Comment writes restart_clickhouse() instead of restart_clickhouse (.claude/CLAUDE.md naming rule) AGREE The description now says restart_clickhouse; the test comment is left as is (nit).

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261206-123600

@groeneai

groeneai commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Master plus a 20 s sleep before restart_clickhouse (standing in for the docker stall) fails 3/3 with both CI signatures. Forcing the restarted node to be the initiator, without the new filter, gives refused and replayed DDL entries in 5/5 runs.
b Root cause explained? Yes. restart_clickhouse reaches SIGTERM only through docker exec calls. In the three CI failures the first one stalled for 12.9-14.4 s on the shared docker daemon, so the ~14 s backup had already finished. Separately, the unfiltered in-flight check was satisfied by the initiator's own ON CLUSTER query before its host-level part had started.
c Fix matches root cause? Yes. Both changes make the precondition hold: the backup now outlives the stall (~105 s, 100 parts + SYSTEM SYNC REPLICA), and the wait targets the host-level part (is_initial_query = 0). No assert is widened, no retry, no tag.
d Test intent preserved / new tests added? Yes. All asserts, the random initiator, the random restarted node, random_sleep(3) and the NoTrashChecker lists are unchanged. If shutdown stopped cancelling backups, the backup would end BACKUP_CREATED and the test would fail. Test-only change.
e Both directions demonstrated? Yes. 20 s delay: master 3/3 FAIL, this PR 3/3 PASS (also 3/3 with 40 s). Forced initiator restart: 24 refusals and every entry replayed without the filter, 0 and none with it (5 runs each).
f Fix is general across code paths? Yes. I checked every test that signals a server and expects cancellation. The single-node test_shutdown_cancel_backup has a ~32 s backup (over 2x the longest stall) and no such failures. The wedged/configurable tests use failpoints, and the KILL QUERY tests have no docker round trip.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A (test-only fix). Both node choices and both initiator choices are exercised in the forced, delayed and natural runs.
h Backward compatible? (maintainer-approved exception only) N/A. No server code, setting, format or default change.
i Invariants and contracts preserved? Yes. The test still creates and drops its own table, and the passing path still cancels within seconds of SIGTERM. The module passes 7/7, and the test passes 10/10 with no patch.

Session id: cron:clickhouse-impl-slot-4:20261206-121100

@clickhouse-gh

clickhouse-gh Bot commented Dec 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ef22b92]

Summary: ✅


AI Review

Summary

The PR stabilizes test_shutdown_cancels_backup by creating 100 parts, synchronizing both replicas before backup, and waiting for the restarted node's internal backup query. The cancellation and cleanup assertions remain intact. No actionable findings.

Missing context / blind spots
  • ⚠️ The targeted integration and flaky-check jobs are pending in the current CI report. Runtime validation relies on the author's documented local before/after runs, delayed-restart reproductions, and repeated passes; completed CI results would confirm sanitizer coverage.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-ci can be tested Allows running workflows for external contributors comp-backup Backup/restore infrastructure and backup formats. labels Dec 6, 2026
@robot-clickhouse
robot-clickhouse added this pull request to the merge queue Dec 6, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-backup Backup/restore infrastructure and backup formats. pr-ci

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant