Skip to content

Fix flaky 01169_old_alter_partition_isolation_stress: orphaned client at teardown - #107883

Merged
PedroTadim merged 5 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-01169-old-alter-partition-orphan-client
Sep 18, 2026
Merged

PedroTadim merged 5 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-01169-old-alter-partition-orphan-client

Conversation

@groeneai

@groeneai groeneai commented Jun 18, 2026 •

Copy link
Copy Markdown
Collaborator

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

Flaky in Stateless tests (..., parallel). thread_insert and thread_select were stopped with kill -TERM $PID, which kills only the bash subshell, not the clickhouse-client in flight. The orphan is reparented and runs its query after the final DROP TABLE, printing Table ... src does not exist or Database ... does not exist to the shared stderr, reported as "having stderror". wait_for_queries_to_finish misses it: a client still in connect/auth is not yet in system.processes.

The loops now stop via a flag file, exiting after their current client finishes. The name uses CLICKHOUSE_TEST_UNIQUE_NAME, so the test stays parallel-safe. #98280 and #99018 worked around this race without removing its source.

Two error-path defects found during review are fixed as well:

  • The flag was raised only after wait $PID_3 && wait $PID_4, unreachable on either failure. Under errexit a nonzero wait $PID_3 does not abort (left operand of &&), it only skips wait $PID_4, so a failed worker was reported as success and teardown began while the other was still mutating src / dst. If wait $PID_4 failed instead, errexit fired before the flag was raised and the loops ran past the end of the test. The flag now comes from an EXIT trap, and each worker is waited for separately so the first failure is the exit status.
  • wait $PID_1 ||: discarded a real failure of thread_insert / thread_select. thread_select is the only check of transient isolation violations during the run, so the test could pass after it had exited on a throwIf.

The orphan is timing dependent and does not reproduce on an idle host, so the evidence is a deterministic A/B rather than a rerun.

Validation and CI evidence

A harness mirroring the final region, with a heartbeat file detecting a loop that outlives the script. Old code: exits 0 on a PID_3 failure, leaves the loop running on a PID_4 failure, exits 0 on a thread_insert failure. Fixed: 3, loop stopped, 9. Each element is load-bearing -- removing the trap re-orphans the loop, restoring && or ||: re-swallows the failures. Against a real server the test passes 5/5 with the reference matched and no surviving clients.

Genuine hits with reruns passing, last 30 days:

The sibling 01169_alter_partition_isolation_stress.sh still uses kill -TERM, but none of its recent failures carry this signature, so it is left for a separate change.


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

… at teardown

The background loops thread_insert and thread_select run an infinite
"while true" of clickhouse-client invocations and are stopped with
"kill -TERM $PID". That signal terminates only the bash subshell running
the loop, not the clickhouse-client child it has in flight. The orphaned
client is reparented, keeps running, and executes its INSERT INTO src /
SELECT after the test's final DROP TABLE (and after the runner drops the
per-test database). The late query then prints
"Table ... src does not exist" (UNKNOWN_TABLE) or
"Database ... does not exist" (UNKNOWN_DATABASE) to the shared stderr,
which the test harness reports as a "having stderror" failure even though
the test logic itself is correct. Reruns pass because the race is timing
dependent. wait_for_queries_to_finish does not help: a client still in the
connect/auth phase is not yet visible in system.processes, so the drain
returns before the orphan reaches the server.

Replace the kill-based stop with a stop flag. Each loop checks the flag
between iterations and exits cleanly after its current client has finished,
and the script then "wait"s for both loops, so no client can outlive the
teardown. The prior fixes (98280 increasing the drain timeout, 99018 making
the final wait non-fatal) worked around this same orphan/drain race without
removing its source.

Observed locally with a debug build: the kill-based version leaked the
exact CI signature ("Table ... src does not exist (UNKNOWN_TABLE)") in
2 of 12 runs; the stop-flag version leaked 0 of 20, and the full test
passes 5/5 with stdout matching the reference.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. A faithful bash harness mirroring the test's thread_insert + kill -TERM + teardown, plus a real-clickhouse repro (while true INSERT loop, kill -TERM, then DROP TABLE/DROP DATABASE), leaks Code: 60. DB::Exception: ... Table ... src does not exist. (UNKNOWN_TABLE) to stderr on demand: 2/12 runs on a debug build. This is the exact CI signature.
b Root cause explained? Yes. kill -TERM $PID terminates only the bash subshell running the infinite loop, not the clickhouse-client child it has in flight. The orphan is reparented, survives wait, and runs its INSERT INTO src / SELECT AFTER the final DROP TABLE (and after the runner drops the per-test DB), printing UNKNOWN_TABLE/UNKNOWN_DATABASE to the shared stderr -> harness reports "having stderror". wait_for_queries_to_finish misses it: a client still in connect/auth is not yet in system.processes, so the drain returns early.
c Fix matches root cause? Yes. Replaces the kill-based stop with a stop flag: each loop checks the flag between iterations and exits cleanly after its current client finishes; the script then waits for both loops, so no client can outlive teardown. This removes the orphan at its source rather than widening a timeout or suppressing stderr.
d Test intent preserved / new tests added? Preserved. No assertions changed. thread_select's throwIf consistency checks and all four final consistency queries run unchanged and their stdout still matches the .reference file exactly. No new test needed (this is a test-harness race, not a server bug). No no-parallel/no-random-* tag added; the stop-flag file name uses CLICKHOUSE_TEST_UNIQUE_NAME so it stays parallel-safe.
e Both directions demonstrated? Yes. Unfixed (kill-based): orphan leak in 2/12 runs. Fixed (stop-flag): 0/20 runs leaked, and the full test passes 5/5 end-to-end with stdout matching the reference and clean stderr.
f Fix is general, not a narrow patch? Scoped deliberately to this test (my task). The sibling 01169_alter_partition_isolation_stress.sh and 02443_detach_attach_partition.sh share the same kill -TERM pattern and likely the same latent race, but their recent CI failures are caused by other PRs' own setting randomization (Code 36/48), not this orphan race, so I did not speculatively change them (per the no-speculative-changes rule).

Session id: cron:clickhouse-worker-slot-0:20260618-144500

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @alesapin — could you review this? Test-only fix for flaky 01169_old_alter_partition_isolation_stress: the background thread_insert/thread_select loops are stopped with kill -TERM, which orphans the in-flight clickhouse-client; it then runs its query after the final DROP TABLE and leaks UNKNOWN_TABLE/UNKNOWN_DATABASE to stderr. Replaced kill -TERM with a stop flag so the loops exit cleanly and no client outlives teardown (parallel-safe, no assertions changed).

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jun 18, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e0cf7b0]

Summary: ✅


AI Review

Summary

This PR replaces the kill -TERM shutdown in 01169_old_alter_partition_isolation_stress.sh with a stop-file/trap flow so in-flight clickhouse-client processes cannot outlive teardown. That teardown fix itself looks sound, but the test still has one remaining false-negative path: thread_partition_src_to_dst swallows every non-SERIALIZATION_ERROR exception and can therefore pass after it has already observed an invariant violation.

Findings

⚠️ Majors

  • [tests/queries/0_stateless/01169_old_alter_partition_isolation_stress.sh:49-67] thread_partition_src_to_dst still wraps its transaction in out=$(...) ||: and then only distinguishes SERIALIZATION_ERROR. If the embedded throwIf fails, or any other exception is returned, the worker still exits 0 and even advances count / sum as if the transaction had committed. That lets the test go green after a real type=3 isolation failure.
    Suggested fix: keep SERIALIZATION_ERROR as the only expected retry, and make every other nonzero result fail the worker immediately. The newer 01169_alter_partition_isolation_stress.sh already has the safer shape.
Final Verdict

Needs one more fix before merge: the teardown race is addressed, but the test still drops one of its own invariant failures.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jun 18, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.30% 85.20% -0.10%
Functions 92.60% 92.50% -0.10%
Branches 77.50% 77.50% +0.00%

Changed lines: No C/C++ source files changed — skipping uncovered code analysis.

Newly covered by added/modified tests: 354 line(s), 11 function(s) across 140 file(s) · Details

Top files
  • src/Storages/ObjectStorageQueue/ObjectStorageQueueIFileMetadata.cpp: 12 line(s), 1 function(s)
  • src/Functions/FunctionsBitmap.h: 11 line(s)
  • src/Functions/LeftRight.h: 11 line(s)
  • src/AggregateFunctions/AggregateFunctionQuantileInterpolatedWeighted.cpp: 8 line(s)
  • src/Storages/MergeTree/MergeTreePartsMover.cpp: 8 line(s)

Full report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI summary — commit 07c4a2f7ee20

CI finished. One failure, not caused by this PR:

The PR's own target test passes across all stateless shards.

groeneai added 2 commits July 29, 2026 14:04
The stop flag was raised only after `wait $PID_3 && wait $PID_4`, which is
unreachable on both error paths.

With errexit, a nonzero `wait $PID_3` does not abort the script because it is
the left operand of `&&`; it merely skips `wait $PID_4`, so the failure is
reported as success and teardown starts while thread_partition_dst_to_src may
still be mutating src / dst. Measured on a harness mirroring the final region:
the script exits 0 where the worker failed with 3.

If instead `wait $PID_4` fails, errexit fires before `touch "$STOP_FILE"`, so
thread_insert and thread_select are never asked to stop. They survive the
script and keep running clickhouse-client against tables the test has dropped,
which is the orphaned-client leak this test was changed to avoid. Measured with
a heartbeat file: the loop keeps appending after the script has exited.

Raise the flag from an EXIT trap so it happens on every path, and wait for each
worker separately so the first failure is the exit status. The trap also drains
the partition workers, so a worker still mid-query when another one fails
finishes inside the trap rather than after teardown. The `||:` on the two loop
waits is dropped: with the flag they exit cleanly, so a nonzero status is a real
failure -- thread_select is the only check of transient isolation violations
during the run, and the four final queries only validate the terminal state.
The comment named the old  form; the reason for the change
belongs in the commit message, not next to the code.
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 6cd876eb88b0d1cd28c7e0b3e0fa2b48a1cd41a5

No failures on this head, so there is nothing to assign an owner to.

Check / test Reason Owner / fixing PR
(none) 174 check-runs completed, 0 failure/timed_out/cancelled -

174 of 174 check-runs completed (56 success, 118 skipped). Config Workflow, Style check,
Fast test, Fast test (arm_darwin) and Finish Workflow all succeeded, and result_pr.json
reports the root as OK with no non-OK child. CIDB agrees: 101078 passing rows across 58 checks
for this commit and 0 rows in FAIL or ERROR.

This head is the trap-based teardown fix answering the two clickhouse-gh[bot] findings on
01169_old_alter_partition_isolation_stress.sh. The stateless shards where the test runs are
green here, including the previously flaky Stateless tests (amd_tsan, parallel).

Session id: cron:our-pr-ci-monitor:20260729-213000

@groeneai

groeneai commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 6cd876e

Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Sync - CH Inc sync (private, not actionable)

Every other dispatched check reported success. This supersedes the earlier ledger on this PR,
which cited a 40-character SHA that does not resolve to any commit here; the covered commit is
the current head 6cd876eb88b0d1a35ad83282fd9543f12a9d11e5.

Session id: cron:our-pr-ci-monitor:20260809-170000

@groeneai groeneai added the groeneai-origin-unknown PR origin: unclassified (legacy task) label Aug 19, 2026
@abashkeev abashkeev added the comp-testing Test content only (no production code). label Sep 4, 2026
@PedroTadim PedroTadim self-assigned this Sep 17, 2026
@PedroTadim

Copy link
Copy Markdown
Member

@groeneai is this pr still relevant?

@groeneai

Copy link
Copy Markdown
Collaborator Author

Yes, still relevant: the defect is live on master and the patch applies unchanged.

01169_old_alter_partition_isolation_stress.sh on master is still the exact blob this PR is based on (a77f5cfb7613, last modified 2026-05-13), so it still stops the two infinite loops with kill -TERM (lines 111-112) and still lets an in-flight clickhouse-client outlive teardown.

CIDB, last 30 days for this test: 4 failed rows, 3 of them this signature, and one of those 3 is on master rather than on a PR.

  1. master, NightlyCoverage amd_llvm_coverage_per_test 4/8, 2026-09-04, sha 55ebb8f7b3e0: Code: 60. Table test_a3j3x3g1r3k6.src does not exist. (UNKNOWN_TABLE) ... (in query: INSERT INTO src VALUES /* (16, 1) */), which is thread_insert's own query arriving after the final DROP TABLE src (report).
  2. Stateless tests (arm_binary, parallel), 2026-09-15: Unknown table expression identifier 'src' from thread_select's "no duplicates" query, 13 out of 13 reruns passed (report).
  3. Stateless tests (arm_asan_ubsan, azure, parallel), 2026-08-29: Code: 636 CANNOT_EXTRACT_TABLE_STRUCTURE from the same loop's merge(currentDatabase(), '') query (report).

Three different error codes, one mechanism: whichever query the orphaned client happens to be running when teardown drops the tables. The 4th row is a server-side abort in another PR's build, unrelated to this.

The branch is mergeable with no conflicts, CI on 6cd876eb88b0 is green (56 success, 120 skipped, 0 failures), and both findings from the automated review are fixed in 4d043b7afdb9. Nothing is pending on my side, so it only needs a review.

}
trap stop_threads EXIT

# One wait per worker, so the first failure becomes the exit status.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

thread_select failures are surfaced now, but the type=3 worker still has the same false-negative shape. thread_partition_src_to_dst wraps its whole transaction in out=$(...) ||: and then only treats SERIALIZATION_ERROR as special. If its internal throwIf fires, or any other exception comes back, the function still returns success and even advances count / sum as if the transaction committed.

That means this test can still go green after it has already observed a real invariant violation on the type=3 path. Please make SERIALIZATION_ERROR the only expected retry and fail the worker on every other nonzero result, like the newer 01169_alter_partition_isolation_stress.sh does.

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.

Checked this against the head before answering. The mechanism is real, the consequence is not.

out=$(...) ||: does discard the worker's status, and count / sum do advance on a non-SERIALIZATION_ERROR result. What that misses is the branch right after it: grep -Fv SERIALIZATION_ERROR | grep -F "Received from " prints the offending exception, and then the src / dst dump, to stdout, and clickhouse-test fails any test whose stdout carries an exception regardless of the exit status (having exception in stdout, tests/clickhouse-test:1539, unconditional check at :3640).

Measured on a local debug server (26.10.1.1), one run per arm:

arm verdict failure reason
head, unmodified [ OK ]
type=3 throwIf forced to fire on every iteration [ FAIL ] having exception in stdout
type=3 throwIf forced on exactly one iteration (i=5) [ FAIL ] having exception in stdout
type=3 client made to fail silently at i=5 (rc=1, empty output) [ FAIL ] having exception in stdout

No failing arm reported a return code: reason, so the status really was 0 in each case and the test failed anyway. The last arm answers the count / sum half: the advance is self-detecting, because the drifted expectation makes the next iteration's own throwIf fire.

So I am leaving thread_partition_src_to_dst as it is. It is unchanged master code, this PR is +32/-6 on the teardown path, and reshaping that worker into the sibling's transactions.lib form is a separate change to a test that is currently green.

@PedroTadim
PedroTadim added this pull request to the merge queue Sep 18, 2026
Merged via the queue into ClickHouse:master with commit 8e93526 Sep 18, 2026
178 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 18, 2026
@clickhouse-gh clickhouse-gh Bot added comp-transactions MergeTree transactions (MVCC): BEGIN / COMMIT / ROLLBACK, the transaction log and manager, part v... and removed comp-testing Test content only (no production code). labels Nov 22, 2026
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-transactions MergeTree transactions (MVCC): BEGIN / COMMIT / ROLLBACK, the transaction log and manager, part v... groeneai-origin-unknown PR origin: unclassified (legacy task) pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants