Skip to content

Make the filesystem cache download wait honor query cancellation - #109116

Merged
murphy-4o merged 2 commits into
ClickHouse:masterfrom
murphy-4o:fs-cache-cancellable-download-wait
Jul 2, 2026
Merged

murphy-4o merged 2 commits into
ClickHouse:masterfrom
murphy-4o:fs-cache-cancellable-download-wait

Conversation

@murphy-4o

@murphy-4o murphy-4o commented Jul 2, 2026 •

Copy link
Copy Markdown
Member

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 a hang where a query reading through the filesystem cache could keep waiting on an in-progress download after being cancelled with KILL QUERY, and where dropping or SYSTEM STOP VIEW-ing a refreshable materialized view whose refresh was stuck in such a wait would block.

Description

When a query needs a range of a file segment that another thread is downloading, it waits in FileSegment::wait(). That wait is only woken on download completion (with a 60s timeout, after which the caller re-loops), and it never checks whether the waiting query has been cancelled. So a stalled or dead downloader can keep a reader waiting for a long time, and KILL QUERY (or pipeline cancellation) has no effect on a thread parked in this wait — cancellation is cooperative and never reaches it.

For a refreshable materialized view this is worse: DROP / SYSTEM STOP VIEW goes through RefreshTask::shutdown() → deactivate(), which waits for the whole in-flight refresh to stop. If that refresh is blocked in the cache wait above, the drop/stop hangs, and RefreshTask::interruptExecution() — which only cancels the pipeline executor — doesn't reach it.

Fix:

  1. FileSegment::wait() checks CurrentThread::get().isQueryCanceled() before entering the wait and throws QUERY_WAS_CANCELLED. The caller (CachedOnDiskReadBufferFromFile::prepareReadFromFileSegmentState) re-enters wait() on each loop, so a cancellation that lands mid-wait is observed on the next round. The 60s timeout and the fast path (cache hit / completed download) are unchanged. This mirrors how StorageSharedMergeTree already guards its own blocking waits.
  2. RefreshTask::interruptExecution() additionally cancels the running refresh query through its process-list entry (not just the pipeline executor), so a refresh blocked in I/O actually stops and shutdown() → deactivate() returns promptly.

Version info

  • Merged into: 26.7.1.435 (included in 26.7 and later)

A reader that finds a file segment DOWNLOADING waits on it in
FileSegment::wait(), which only woke on download completion (60s timeout,
then the caller re-loops). So a stalled or dead downloader could block the
reader for a long time even after its query was cancelled (KILL QUERY) —
pipeline cancellation never reaches a thread parked in this wait.

Check CurrentThread::get().isQueryCanceled() before entering the wait and
throw QUERY_WAS_CANCELLED; the caller re-enters wait() each loop, so a cancel
that lands mid-wait is caught next round. This mirrors how StorageSharedMergeTree
guards its own blocking waits.

Also cancel the running refresh query in RefreshTask::interruptExecution (not
just the pipeline executor), so dropping/stopping a refreshable materialized
view whose refresh is blocked in such a wait returns promptly instead of
blocking shutdown() -> deactivate() on the whole in-flight refresh.

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

clickhouse-gh Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f60e231]

Summary: ❌

job_name test_name status info comment
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/new_tests_check.py FAIL IGNORED

AI Review

Summary

This PR makes FileSegment::wait() poll query cancellation once per second and makes refresh shutdown kill the in-flight refresh query through its QueryStatus, which addresses the two inline correctness issues from the previous review. I did not find a surviving bug in the current code, but the PR still lacks focused regression coverage for the blocked filesystem-cache wait / refreshable materialized view shutdown path, so the prompt-cancellation contract is not proven yet.

Missing context / blind spots
  • ⚠️ The Praktika report attached to this draft PR currently has no test results, so there is no CI evidence for the new cancellation paths yet.
Tests
  • ⚠️ [src/Interpreters/FileCache/FileSegment.cpp:555, src/Storages/MaterializedView/RefreshTask.cpp:1516] The fix changes timing-sensitive cross-thread cancellation behavior, but the diff adds no test that forces a reader to sleep in FileSegment::wait() and then proves that KILL QUERY / max_execution_time and refreshable-MV SYSTEM STOP VIEW / DROP unblock within a bounded time. A focused integration test in test_filesystem_cache, plus an RMV variant if the same fixture cannot cover both, would close this evidence gap.
Final Verdict

Needs focused regression coverage before merge.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 2, 2026
FileSegment::wait() parked in a single cv.wait_for(..., 60s) that is only
woken on download progress, so a thread already asleep there did not observe
query cancellation until the timeout expired. DROP / SYSTEM STOP VIEW of a
refreshable materialized view whose refresh was stuck in this wait could sit
in deactivate() for up to a minute.

Wait in 1s slices and re-check the query's QueryStatus each slice via
throwIfKilled(), which also re-raises the original cancellation reason
(KILL QUERY / max_execution_time / ...) instead of a generic
QUERY_WAS_CANCELLED. The overall 60s budget and the cache-hit fast path are
unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@murphy-4o
murphy-4o marked this pull request as ready for review July 2, 2026 08:24
@clickhouse-gh

clickhouse-gh Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.50% 85.50% +0.00%
Functions 92.60% 92.70% +0.10%
Branches 77.70% 77.70% +0.00%

Changed lines: Changed C/C++ lines covered: 36/37 (97.30%) · Uncovered code

Full report · Diff report

@kssenii kssenii self-assigned this Jul 2, 2026
@murphy-4o
murphy-4o enabled auto-merge July 2, 2026 13:08
@murphy-4o
murphy-4o added this pull request to the merge queue Jul 2, 2026
Merged via the queue into ClickHouse:master with commit c305cbe Jul 2, 2026
344 of 348 checks passed
@murphy-4o
murphy-4o deleted the fs-cache-cancellable-download-wait branch July 2, 2026 13:37
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 2, 2026
evillique added a commit that referenced this pull request Aug 17, 2026
Resolve conflicts and carry the `RefreshTask` part of #109116, which introduced
`ExecutionState::executing_query_status` and is absent from 25.8.
evillique added a commit that referenced this pull request Aug 17, 2026
Resolve conflicts and carry the `RefreshTask` part of #109116, which introduced
`ExecutionState::executing_query_status` and is absent from 26.6.
evillique added a commit that referenced this pull request Aug 17, 2026
Resolve conflicts and carry the `RefreshTask` part of #109116, which introduced
`ExecutionState::executing_query_status` and is absent from 26.5.
evillique added a commit that referenced this pull request Aug 17, 2026
Resolve conflicts and carry the `RefreshTask` part of #109116, which introduced
`ExecutionState::executing_query_status` and is absent from 26.3.
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ble-download-wait

Make the filesystem cache download wait honor query cancellation
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.

3 participants