Skip to content

Fix a missing --chime when a slow query ends with an error - #117179

Merged
alexey-milovidov merged 2 commits into
masterfrom
fix-chime-elapsed-on-error-path
Aug 31, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
fix-chime-elapsed-on-error-path

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 30, 2026 •

Copy link
Copy Markdown
Member

Related: #92718
Related: #104545

Description

--chime decided whether a query ran long enough by comparing its threshold against ProgressIndication::elapsedSeconds, which prefers the elapsed time reported by the server over the client's own clock. The server-reported value only advances when a Progress packet arrives, and TCPHandler::processOrdinaryQuery sends a final Progress packet only on the success path - the exception path rethrows without one. So after a failed query the client's notion of the elapsed time is whatever the last periodic packet happened to carry: normally one interactive_delay behind, but unbounded when the handler's loop iteration is delayed, because once the pipeline has recorded the exception the next pull throws before the loop reaches sendProgress. A query that visibly ran past the threshold could therefore finish with no chime.

Measured against a local server (the reported time is --time, the real one is /usr/bin/time):

success path, interactive_delay=1s, sleep 2.5s:  reported 2.501  (real 2.54)
error   path, interactive_delay=1s, sleep 2.5s:  reported 2.001  (real 2.58)

The chime is now gated on the client's own clock (ProgressIndication::clientElapsedSeconds). That is the wall-clock time the user actually waited for, which is what the chime is about, and it does not depend on the timing of progress packets. The server-reported time is still what Elapsed:, --time and the rows/bytes-per-second calculations use, so this does not change any other output; the residual staleness of those numbers after an error is pre-existing and left alone.

This is what made 04312_client_chime_on_slow_query_92718 flaky. Case 11 exercises the error path over a pty with --chime 1 against a ~1.5-second query, a 0.5-second margin; on a loaded CI machine the last progress packet can predate the 1-second mark, and the case reports no BEL. Every recorded failure of this test on master and in pull requests over the last 30 days is case 11 and nothing else.

The failure reproduces on demand by making the server send exactly one progress packet, below the threshold, and none after it - 2 * 0.9s is past the moment the query throws:

--chime 1, SELECT throwIf(sleep(1.5) = 0, ...), interactive_delay=100000 -> BEL
--chime 1, SELECT throwIf(sleep(1.5) = 0, ...), interactive_delay=900000 -> no BEL
--chime 1, SELECT sleep(1.5),                   interactive_delay=900000 -> BEL

That is added as case 13, so the test now covers the contract that used to be flaky rather than relying on a timing margin. Cases 4 and 11 also stopped identifying the expected failure by the expected error message: the client echoes the failed query text, which contains that message, so the check passed for any failure at all - they now match on the FUNCTION_THROW_IF_VALUE_IS_NON_ZERO error code name.

CI report for the failure this fixes: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=9904bd2786b3927e436b043c555231828d3f069f&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_tsan%2C%20s3%20storage%2C%20parallel%2C%201%2F3%29

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed clickhouse-client and clickhouse-local not emitting the --chime alert for a long-running query that ends with an error. The threshold was checked against the query time last reported by the server, which is not refreshed when a query fails, so it could be arbitrarily behind the real duration of the query. It is now checked against the time the client actually waited.


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

The chime compared its threshold against `ProgressIndication::elapsedSeconds`,
which prefers the elapsed time reported by the server. That value only advances
when a `Progress` packet arrives, and `TCPHandler::processOrdinaryQuery` sends a
final `Progress` packet only on the success path - its exception path rethrows
without one. So after a failed query the client's notion of the elapsed time is
whatever the last periodic packet carried: normally one `interactive_delay`
behind, but unbounded when the handler's loop iteration is delayed, because once
the pipeline has recorded the exception the next `pull` throws before the loop
reaches `sendProgress`. A query that visibly ran past the threshold could
therefore finish with no chime.

Gate the chime on the client's own clock instead. That is the wall-clock time the
user actually waited for, which is what the chime is about, and it does not
depend on the timing of progress packets. `Elapsed:`, `--time` and the
throughput calculations keep using the server-reported time, so no other output
changes.

This is what made `04312_client_chime_on_slow_query_92718` flaky: case 11
exercises the error path over a pty with `--chime 1` against a ~1.5-second
query, and on a loaded machine the last progress packet can predate the
1-second mark. Every recorded failure of the test over the last 30 days is
case 11 and nothing else.

Case 13 turns that into a deterministic check by raising `interactive_delay` so
the server sends exactly one `Progress` packet, at ~0.9s, and none after it.
Cases 4 and 11 also stopped identifying the expected failure by the
`expected error` message, which the client's echo of the failed query text made
vacuous, and now match on `FUNCTION_THROW_IF_VALUE_IS_NON_ZERO`.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=9904bd2786b3927e436b043c555231828d3f069f&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_tsan%2C%20s3%20storage%2C%20parallel%2C%201%2F3%29

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3d787b7]

Summary: ✅


AI Review

Summary

This PR switches the --chime threshold check from the server-reported elapsed time to the client-side wall clock so a slow query that ends with an exception still rings even when no final Progress packet arrives. The implementation change itself looks correct, but the new regression coverage is still narrow enough that the pre-fix code can pass on a loaded runner, so the contract is not pinned down yet.

Findings
⚠️ Majors
  • [tests/queries/0_stateless/04312_client_chime_on_slow_query_92718.sh:166] Case 13 still uses interactive_delay = 900000 against --chime 1. TCPHandler emits that lone pre-error progress update only after executor.pull(..., 900ms) times out, and that timeout is implemented through std::condition_variable::wait_for, so scheduler slip can legally push the wakeup past 1.0 s. When that happens, the old elapsedSeconds-based code already sees elapsed_ns >= 1s and rings, so this regression passes without the fix. Widen the margin so the only pre-error progress packet is forced comfortably below the threshold.
Final Verdict

⚠️ Needs changes before merge: the client-side timing fix looks right, but the new regression case is still not deterministic enough to prove it.

LLVM Coverage Report

Measured on commit 3d787b7.

Metric Baseline Current Δ
Lines 88.60% 88.60% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 80.90% 80.90% +0.00%

Changed lines: Changed C/C++ lines covered: 8/8 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 30, 2026
# clock this case reports `no BEL`, which is also what made case 11 flaky under CI load: there the
# same staleness comes from a delayed `TCPHandler` loop iteration rather than from a large
# `interactive_delay`.
/usr/bin/script -eqc "${CLICKHOUSE_CLIENT} --chime 1 -q \"SELECT throwIf(sleep(1.5) = 0, 'expected error') SETTINGS interactive_delay = 900000\"" /dev/null > "$tty13" 2>&1

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.

TCPHandler only emits that progress update after executor.pull(block, interactive_delay / 1000) times out, and PullingAsyncPipelineExecutor implements the timeout via ConcurrentBoundedQueue::tryPop(..., milliseconds) / std::condition_variable::wait_for. With --chime 1 and interactive_delay = 900000, the old code is only guaranteed to stay below the threshold if that first 900 ms wait wakes within ~100 ms of schedule. On a loaded runner it can legally slip past 1.0 s, at which point the lone Progress packet already carries elapsed_ns >= 1s and the pre-fix client rings, so this regression passes without the fix. Please widen the gap here so the last pre-error progress update is forced safely below the threshold.

@clickhouse-gh

clickhouse-gh Bot commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 3d787b73c with master bb113c384 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of d469feea5; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

22 translation units recompiled, 122 s compile time in total, 22 of them have a recent master baseline.

Job report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 31, 2026
@alexey-milovidov alexey-milovidov self-assigned this Aug 31, 2026
Merged via the queue into master with commit f9990c8 Aug 31, 2026
175 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-chime-elapsed-on-error-path branch August 31, 2026 15:27
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default 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.

2 participants