Skip to content

Check isCanceled before calling eof on the input ReadBuffer in TCPHandler#100666

Merged
alexey-milovidov merged 1 commit intomasterfrom
fix-readbuffer-canceled-eof-check
Mar 27, 2026
Merged

Check isCanceled before calling eof on the input ReadBuffer in TCPHandler#100666
alexey-milovidov merged 1 commit intomasterfrom
fix-readbuffer-canceled-eof-check

Conversation

@alexey-milovidov
Copy link
Copy Markdown
Member

When waiting for a new query between requests, TCPHandler::runImpl calls in->eof() to detect client disconnection. If the ReadBuffer was previously canceled (due to a failed read during a prior query's callback), calling eof() triggers next(), which hits chassert(!isCanceled()) and aborts the server.

Add an in->isCanceled() check before in->eof() in the condition, matching the pattern already used in receivePacketsExpectCancel. Also guard the in->eof() call in the LOG_TEST message to avoid evaluating it on a canceled buffer (function arguments are not short-circuit evaluated unlike ||).

This is a follow-up to #98955 and #99272 which fixed similar race conditions in callback paths.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=99537&sha=7338429ff8d3349ba147f9ba05558bbd6b84e4b2&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29

Closes #100581

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):

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

…`TCPHandler`

When waiting for a new query between requests, `TCPHandler::runImpl`
calls `in->eof()` at line 495 to detect client disconnection. If the
`ReadBuffer` was previously canceled (e.g., due to a failed read during
a prior query's callback), calling `eof()` triggers `next()`, which hits
`chassert(!isCanceled())` and aborts the server.

Add an `in->isCanceled()` check before `in->eof()` in the condition,
matching the pattern already used in `receivePacketsExpectCancel`. Also
guard the `in->eof()` call in the `LOG_TEST` message to avoid
evaluating it on a canceled buffer (function arguments are not
short-circuit evaluated unlike `||`).

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=99537&sha=7338429ff8d3349ba147f9ba05558bbd6b84e4b2&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh bot commented Mar 25, 2026

Workflow [PR], commit [1aa7ce7]

Summary:


AI Review

Summary

This PR fixes a real correctness issue in TCPHandler: it avoids calling in->eof() on a canceled input buffer by checking in->isCanceled() first, and it also protects the LOG_TEST argument evaluation path. The change is minimal, targeted, and consistent with existing handling in receivePacketsExpectCancel. I did not find additional correctness, safety, performance, or compatibility issues in the diff.

ClickHouse Rules
Item Status Notes
Deletion logging
Serialization versioning
Core-area scrutiny
No test removal
Experimental gate
No magic constants
Backward compatibility
SettingsChangesHistory.cpp
PR metadata quality
Safe rollout
Compilation time
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh bot added the pr-ci label Mar 25, 2026
Copy link
Copy Markdown
Member

@Algunenano Algunenano left a comment

Choose a reason for hiding this comment

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

Same #100585

@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh bot commented Mar 25, 2026

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.20% 84.10% -0.10%
Functions 24.40% 24.30% -0.10%
Branches 76.70% 76.70% +0.00%

PR changed lines: PR changed-lines coverage: 100.00% (9/9, 0 noise lines excluded)
Diff coverage report
Uncovered code

@nikitamikhaylov
Copy link
Copy Markdown
Member

Similar #100536

@alexey-milovidov alexey-milovidov self-assigned this Mar 27, 2026
@alexey-milovidov alexey-milovidov added this pull request to the merge queue Mar 27, 2026
Merged via the queue into master with commit 3769039 Mar 27, 2026
153 checks passed
@alexey-milovidov alexey-milovidov deleted the fix-readbuffer-canceled-eof-check branch March 27, 2026 20:35
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 27, 2026
Desel72 pushed a commit to Desel72/ClickHouse that referenced this pull request Mar 30, 2026
…anceled-eof-check

Check `isCanceled` before calling `eof` on the input `ReadBuffer` in `TCPHandler`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Logical error: 'ReadBuffer is canceled. Can't read from it.' (STID: 2508-2913)

4 participants