Repository navigation
Throw cancellation in ReadBuffer::next - #109785
Michicosun wants to merge 4 commits into
Conversation
|
Workflow [PR], commit [10a3784] Summary: ❌
AI ReviewSummaryThis PR adds a cancellation check to Findings
Final VerdictChanges requested. |
|
|
||
| bool ReadBuffer::next() | ||
| { | ||
| CurrentThread::checkIfNotCancelled(); |
There was a problem hiding this comment.
This changes the cleanup contract for every ReadBuffer, but only TCPHandler::skipData got an exemption. The HTTP exception path still drains unread POST bodies through the same ReadBuffer before reusing the socket (src/Server/HTTP/sendExceptionToHTTPClient.cpp:28-51, called from src/Server/HTTPHandler.cpp:567 and src/Server/HTTPHandler.cpp:586). After KILL QUERY or a timeout in throw mode, input_stream->isCanceled() is still false, so ignoreAll() reaches ReadBuffer::next() and this new checkIfNotCancelled() rethrows instead of draining. The catch there then flips Connection: close, so canceled or timed-out HTTP uploads lose keep-alive even though the body was still drainable. TCPHandler now blocks cancellation around skipData; HTTP needs the same treatment (or this check needs to be narrower) to avoid regressing connection reuse.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 18/18 (100.00%) · Uncovered code |
A query killed or detached while loading skip-index marks kept reading from object storage for minutes afterwards, because nothing between the marks loader and S3 observes QueryStatus::is_killed. Reported by alexey-milovidov on ClickHouse#113011: a query from 04010_text_index_incompelete_granules.sql sat for ~815 s with is_cancelled = 1 and read_rows = 0, tripping the hung check. In that run's hung_check.log the query thread waits on the marks-load future during skip-index analysis while the marks pool thread is inside ReadBuffer::next below MergeTreeMarksLoader::loadMarksImpl, in ReadBufferFromRemoteFSGather -> CachedOnDiskReadBufferFromFile -> S3. throwIfKilled and checkIfNotCancelled appear zero times in the gather, cached, encrypted-file and file-view read buffers, and under the thread fuzzer every buffer fill costs an injection sleep. Two nearer candidates do not work. The loader's own is_canceled flag is written only by ~MergeTreeMarksLoader, which cannot run while loadMarks() is on the stack, so it can only drop a task still queued in the pool. And the granule loop in loadMarksImpl runs at most 26 trivial iterations over a ~624 byte marks file in this carrier, while the whole wait sits inside one reader->eof() several frames lower. The interruption point therefore has to be in the read buffer. ReadBufferFromRemoteFSGather::readImpl now calls throwIfKilled before current_buf->next(). readImpl is the complete set of blocking-fill sites: it has exactly two callers, both in nextImpl, and everything nextImpl does before the first is non-blocking, so a check at the top of nextImpl would be redundant. The short-forward-seek drain in seek() is covered transitively, since ReadBuffer::ignore loops on next -> nextImpl -> readImpl. The throw precedes the SwapHelper, so the buffer is never left partially advanced. The check is gated on an opt-in ReadSettings flag that only makeIndexReaderStream sets, on its own copy of the settings, so every other read is byte-identical. That scoping is required rather than stylistic: QUERY_WAS_CANCELLED is not in isRetryableException, so an unscoped throw makes CHECK TABLE plus KILL delete a healthy part, and ATTACH plus KILL rename one broken-on-start. The generic form of this change, ClickHouse#109785, had to invent a thread-local blocker for exactly those reads. The QueryStatusPtr is resolved in ReadPipeline::build on the calling thread and threaded in, beside the existing query_id hoist that exists for the same reason: gather creators run lazily and may execute on threadpool workers with no query context, where tryGetQueryContext returns null and the check would be silently inert. The new test is deterministic via a PAUSEABLE failpoint placed inside the opt-in branch, so reaching it also proves the marks reader asked for interruptible reads: park the marks read, KILL, single-step exactly one fill with SYSTEM NOTIFY FAILPOINT, then assert the read unwinds instead of fetching another buffer. Removing only throwIfKilled, and removing only the opt-in, each redden it with a distinct message. Index-analysis cancellation granularity as a whole, and the experimental use_reader_executor path, are deliberately out of scope. This removes the "ignores cancellation" half only, so it does not on its own clear the hung check class. Co-Authored-By: Claude Fable 5 <[email protected]>
|
Dear @Michicosun, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message. |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
This patch adds a cancellation point to
ReadBuffer::next, let's check how many tests will fail :)