Skip to content

Do not let a stateless test take the harness's bash xtrace descriptor - #115917

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:test-05024-xtrace-fd-collision
Aug 22, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:test-05024-xtrace-fd-collision

Conversation

@groeneai

@groeneai groeneai commented Aug 22, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #115715

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

05024_local_listener_survives_handler_exception exits 141 on Fast test (arm_darwin). It landed with
#115715 and reddened four unrelated PRs in one 2h20m window; Fast test gates every PR. Two of those runs:
report 1,
report 2.

141 is 128 + 13, death by SIGPIPE, from a descriptor collision with the harness itself.
shell_config.sh opens the bash xtrace log on fd 3 and sets BASH_XTRACEFD=3. The test then did
exec 3<>/dev/tcp/..., closing that log and rebinding fd 3 to the native port, so bash wrote every xtrace
record into the socket. The test sends HEAD precisely so the server rejects it as an unexpected packet and
closes the connection, and the tracer's next write lands on a closed socket. That also explains the
bashlogs ending mid-script: the line that stole the descriptor is the last thing the trace could record, so
the truncation points at the hijack, not at where execution stopped.

The fix lets bash allocate the descriptor (exec {sock}<>..., as
04306_insert_stream_timeout_flush_http_input.sh already does) and ignores SIGPIPE around a poke whose
whole point is that the peer goes away. || true does not work here: the signal kills the shell before the
|| list is evaluated.

Every traced run hijacks the descriptor on Linux too; only whether the peer closes early enough to be
fatal is racy, hence macOS. And because the tracer shared the socket, the packet the server received was
xtrace text wrapped around the intended HEAD, so this is a descriptor fix, not signal tolerance. Only
this test collides, so the harness is unchanged.

Validation: 60/60 141 before and 60/60 clean after on the isolated mechanism, 10/10 to 10/10 against a
real clickhouse-local native port, 150/150 green across randomized and Fast test-shaped runs. The fixed
test still fails against a pre-#115715 binary, so it is not disarmed.


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

Version info

  • Merged into: 26.8.1.1922 (included in 26.8 and later)

05024_local_listener_survives_handler_exception exits 141 on Fast test
(arm_darwin). 141 is 128 + 13, i.e. death by SIGPIPE, and the cause is a
descriptor collision with the test harness itself.

shell_config.sh opens the bash xtrace log on fd 3 and points the tracer at it
with BASH_XTRACEFD=3. The test then did `exec 3<>/dev/tcp/...`, which closes
that log and rebinds fd 3 to the ClickHouse native port, so bash writes every
xtrace record into the socket. The test sends HEAD precisely so the server
treats it as an unexpected packet and closes the connection, which is the
scenario under test; the tracer's next write then lands on a closed socket and
the shell itself takes the signal. Because the fatal write is the tracer's, the
death belongs to no command in the script, and the trace stops at the line that
stole its descriptor, which is why both CI bashlogs end mid-script at that exec
rather than at the point execution actually stopped. CI passes --trace, so this
half is live on the failing check. No pipefail is involved.

Letting bash allocate the descriptor removes the collision: the first
auto-assigned fd is 10, so it can collide neither with the harness's fd 3 nor
with stdin/stdout/stderr. 04306_insert_stream_timeout_flush_http_input.sh
already writes it this way. SIGPIPE is ignored around the poke because the peer
going away is the expected outcome of the scenario rather than a failure, and
the write's stderr is discarded because clickhouse-test fails a test on any
non-empty stderr, so a surviving "write error: Broken pipe" would only trade one
red for another. `|| true` does not work here: the signal kills the shell before
the || list is evaluated.

The collision is platform-independent, and every traced run hijacks the
descriptor; what differs is whether the peer closes early enough for a tracer
write to land on a dead socket. Against a peer that closes on accept this
reproduces 60/60; against a real clickhouse-local native port on Linux it needs
an extra write to become deterministic, which is consistent with the failure
being seen on macOS only. The precise reason darwin loses that race more often
is not measured and is not claimed.

The hijack had a second consequence: on runs where the connection stayed up, the
tracer's records went into the native port too, so the packet the server
received was xtrace text wrapped around the intended HEAD. The test's premise of
sending exactly one unexpected packet therefore did not hold on any traced run,
including the passing ones. That is why this changes the descriptor instead of
merely tolerating the signal.

The reference output is unchanged, and the fixed test still fails against a
pre-115715 binary, so the oracle it exists for is intact.

Related: ClickHouse#115715
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding labels Aug 22, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: 0 blockers, 0 majors, 2 nits (1 fixed, 1 noted)

Two independent reviews of this diff: a cold code review, and a second-model gate pass
(codex, $1.69, 0 findings). Nothing blocking. The diff is one test file, +11/-9,
.reference unchanged, git diff -- src programs empty.

Verified against the tree rather than taken on trust:

  • shell_config.sh:274 opens the xtrace log on fd 3 and :294 sets BASH_XTRACEFD=3, guarded on
    CLICKHOUSE_BASH_TRACING_FILE; tests/clickhouse-test:3715 exports that variable under --trace
    and ci/jobs/fast_test.py:375 passes --trace. The collision is live on exactly the failing check.
  • set -e, set -u and pipefail are all absent from shell_config.sh, which independently rules
    out the head -c 20 + pipefail reading of the failure.
  • TCPHandler::receiveHello (src/Server/TCPHandler.cpp:2073-2082) special-cases a leading G or
    P into a polite 400 Bad Request, and throws UNEXPECTED_PACKET_FROM_CLIENT otherwise, so the
    choice of HEAD is load-bearing and the surviving comment is accurate.
  • man bash guarantees {varname} allocates a descriptor >= 10; probed with fd 3 held by a tracer,
    the allocation is 10. varredir_close is unset in tests/, which is what the two explicit closes
    exist for.
  • Carrier survey reproduced independently: an fd-3 redirection now appears nowhere under
    tests/queries/0_stateless/, with a positive control confirming the pattern still matches
    shell_config.sh:274. 03802_insert_stream_timeout_flush.sh:49-51 carries a comment naming this
    same hazard, written before this bug, which is an independent in-tree witness to the mechanism.
# Finding Verdict
💡 1 trap '' PIPE is not restored after the poke, so SIGPIPE stays ignored for the remainder of the script, including the final curl. Disagree (noted). It cannot mask the oracle. A dead listener fails through two paths that do not involve SIGPIPE: the listener still serving line goes missing so the reference mismatches, and curl's stderr becomes non-empty, which tests/clickhouse-test:3410-3427 turns into a STDERR failure even at exit 0. curl's stdout is a redirected file, never a pipe. Both negative controls (closed port; pre-#115715 binary) redden as required.
💡 2 ### Description ran slightly over the length budget. Agree, fixed before publication.

shellcheck on the fixed file reports only SC1091 (unfollowed source, universal in this suite) and
SC2015 on line 68, which is pre-existing on master and untouched here. The grouped
{ printf ...; } 2>/dev/null form is deliberate: the inline >&${sock} 2>/dev/null spelling is an
SC2261 error, reproduced in both orderings.

Session id: cron:clickhouse-review-slot-8:20260822-141502

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (a-i)
# Gate Answer
a Deterministic repro? Yes. Pre-fix poke block against a peer that closes on accept: 60/60 rc=141, xtrace log truncated 60/60. A dedicated arm (close settled, then two writes) gives 20/20 rc=141. Against a real clickhouse-local native port: 10/10 rc=141.
b Root cause explained? Yes. shell_config.sh opens the bash xtrace log on fd 3 and sets BASH_XTRACEFD=3; the test's exec 3<>/dev/tcp/... closed that log and rebound fd 3 to the native port, so bash's tracer wrote into the socket. The test sends HEAD so the server rejects the packet and closes the connection, and the tracer's next write lands on a closed socket, so the shell takes SIGPIPE. CI passes --trace, so the xtrace half is live on the failing check.
c Fix matches root cause? Yes. The collision is removed (bash-allocated descriptor, first auto fd measured as 10) rather than worked around. No widened bound, no reduced dataset, no no-fasttest, no darwin skip. || true was measured ineffective (20/20 rc=141) because SIGPIPE kills the shell before the || list runs, so the signal is ignored instead.
d Test intent preserved / tests added? Preserved and strengthened. Pre-fix the server received xtrace text wrapped around the intended HEAD on every traced run; post-fix it receives exactly the HEAD (byte-level capture by a recording listener). .reference unchanged. No no-parallel tag.
e Both directions demonstrated? Yes. Same real clickhouse-local native port, arms differing only by the fix: 10/10 rc=141 before, 10/10 rc=0 after. Isolated mechanism: 60/60 rc=141 before, 60/60 rc=0 after.
f General across code paths? Yes. Invariant: a stateless test must not bind a descriptor the harness already owns. Tree-wide survey of tests/queries/0_stateless/*.sh for an fd-3 redirection now returns 0 files (grep validated by a positive control: 5 hits on the pre-fix file). No other numeric fd 3-9 exec redirection exists in the suite. /dev/tcp users are this test and 04306_insert_stream_timeout_flush_http_input.sh, which already uses the auto-fd form and is the prior art adopted here.
g General across inputs? Yes. All peer-timing regimes covered: peer closes on accept, peer stays open through the response, and close settled mid-window. Plus connect failure (points at a closed port: rc=1 with failed to connect to the native port) and process death (see h/negative control). Platform-independent by construction: the collision existed identically on Linux.
h Backward compatible? N/A. One test file, no source change (git diff -- programs src is empty), no setting, no serialization, no SettingsChangesHistory.cpp entry.
i Invariants and contracts preserved? Yes. trap '' PIPE coexists with the existing trap cleanup EXIT (verified: the ports file and fifo are still removed). The descriptor is closed after the poke, no fd leak. Ignoring SIGPIPE masks nothing at the final curl, whose assertion is its output: with the listener absent, or against a pre-#115715 binary, the test still produces a mismatch.

Negative controls (the fix must not disarm the test). Against a pre-#115715 binary, i.e. the state
this test was written to catch, the fixed test still fails 10/10 with died from signal 6. With the poke
pointed at a closed port it exits 1 and its output differs from .reference. With --trace the bashlog now
runs to the end of the script instead of truncating at the hijack.

Randomized runs. 150/150 green: 50 with randomized settings and --jobs 8, 50 in the CI Fast test
shape (--no-random-settings --no-random-merge-tree-settings --order random), 50 repeat. No rc=141, no
non-empty stderr, no reference mismatch.

@clickhouse-gh

clickhouse-gh Bot commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [17532e9]

Summary: ✅


AI Review

Summary

This PR fixes 05024_local_listener_survives_handler_exception.sh so the test no longer steals the harness xtrace descriptor when it opens /dev/tcp, by switching to a bash-allocated fd and tolerating the expected SIGPIPE from the deliberate wrong-protocol poke. I checked the current script against the relevant harness logic in tests/queries/shell_config.sh, the darwin fast-test invocation, and TCPHandler::receiveHello, and I did not find an unresolved correctness, compatibility, or coverage issue in the current version.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Aug 22, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @alexey-milovidov, could you review this? The test took fd 3 for its /dev/tcp socket, which is the descriptor shell_config.sh gives bash's xtrace via BASH_XTRACEFD, so under --trace the tracer wrote into the native port and took SIGPIPE when the server closed it; letting bash allocate the descriptor removes the collision.

@alexey-milovidov alexey-milovidov self-assigned this Aug 22, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 22, 2026
Merged via the queue into ClickHouse:master with commit 0abbc75 Aug 22, 2026
184 of 186 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 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 groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding 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.

3 participants