Skip to content

Fix async bounded read buffer readbigat race - #112573

Merged
kssenii merged 8 commits into
masterfrom
fix-async-bounded-read-buffer-readbigat-race
Aug 12, 2026
Merged

kssenii merged 8 commits into
masterfrom
fix-async-bounded-read-buffer-readbigat-race

Conversation

@kssenii

@kssenii kssenii commented Jul 30, 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):

Fix AsynchronousBoundedReadBuffer's readBigAt data race. Closes #109678.

Version info

  • Merged into: 26.8.1.1240 (included in 26.8 and later)
  • Backported to: 26.7.4.27, 26.6.3.51

kssenii and others added 2 commits July 30, 2026 12:41
readBigAt may be called concurrently from multiple threads (e.g. by
ParallelReadBuffer), but it consumed the pending prefetch_future without
synchronization: two threads could both pass the validity check and both
call get on the same future, which is undefined behavior and aborts in
practice.

Now the prefetch consumption is guarded by a mutex, and prefetch
publishes the file range it may cover, so that readBigAt callers whose
range falls outside of it skip the lock and read directly from impl
without waiting for the prefetch (readBigAt of every impl supporting it
uses per-call state, independent of the sequential read path). A
non-overlapping readBigAt also no longer drops the prefetch - it is left
pending for a reader of the covered range.

Adds a unit test reproducing the race: with a prefetch in flight,
concurrent readBigAt calls used to abort with SIGABRT.

Co-Authored-By: Claude Fable 5 <[email protected]>
…he submit offset

The result's offset field already accounts for the page cache block
alignment (see ThreadPoolRemoteFSReader::execute), so the served
prefetch range always begins at the request offset and the aligned
window begin only made the window over-inclusive.

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [762fa93]

Summary: ❌

job_name test_name status info comment
Integration tests (arm_binary, distributed plan, 1/4) FAIL
test_storage_nats/test_nats_jet_stream.py::test_nats_restore_failed_connection_without_losses_on_write FAIL cidb IGNORED
Stress test (arm_asan_ubsan, s3) FAIL
UndefinedBehaviorSanitizer: undefined behavior (STID: 0250-4224) FAIL cidb, issue ISSUE CREATED
Bugfix validation (unit tests) ERROR
Bugfix validation (unit tests) ERROR IGNORED

AI Review

Summary

This PR serializes consumption of an in-flight prefetch in AsynchronousBoundedReadBuffer::readBigAt, retains consumed prefetched bytes for later covered reads, and guards lazy file_size initialization in CachedOnDiskReadBufferFromFile, with focused regression tests for both paths. I did not find a remaining correctness or concurrency issue in the current head.

Missing context / blind spots
  • ⚠️ When I reviewed commit 762fa93f05ca081011efec5fef767e3c36be6853, Fast test, Style check, and Build (arm_tidy) were green, but the broader build/ASan/TSan/MSan matrix was still pending, so I could not use those results as additional evidence.
Final Verdict

✅ No blocking findings at the current head.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.70% 86.70% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 79.00% 79.10% +0.10%

Changed lines: Changed C/C++ lines covered: 241/255 (94.51%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.60% 78.60% +0.00%

Changed lines: Changed C/C++ lines covered: 75/81 (92.59%) · Uncovered code

Full report · Diff report

readBigAt now always consumes (drops) an in-flight prefetch before
reading from impl, because readBigAt of impl must not run concurrently
with the sequential prefetch read (e.g. ReadBufferFromHDFS shares one
hdfsFile between hdfsRead and hdfsPread). Concurrent readBigAt callers
race on the consumption, so it is guarded by a mutex, and an atomic
flag keeps the common no-prefetch case lock-free. The previous logic
serving the head of the range from the prefetched buffer (with the
progress shifting for ParallelReadBuffer) is removed.

Also guard the lazily initialized file_size of
CachedOnDiskReadBufferFromFile with a mutex: tryGetFileSize may be
called from concurrent readBigAt calls. Add a test reading through
AsynchronousBoundedReadBuffer over a cached buffer concurrently with
a prefetch in flight.

Co-Authored-By: Claude Fable 5 <[email protected]>
@kssenii
kssenii marked this pull request as draft August 4, 2026 10:09
Restore the logic serving readBigAt from the prefetched buffer, removed
in the previous commit: dropping the prefetch unconditionally regressed
the small-object object storage path (a duplicate range request on
S3/Azure/HTTP after waiting for the prefetch anyway). Moreover, the
consumed prefetch data is now retained in a member, so all readBigAt
calls whose range starts inside it are served from it, not only the one
that consumed the prefetch. The retained data is immutable once
published (via the store to the atomic prefetch_pending) and is only
invalidated by the sequential prefetch before it reuses the buffer.

Adds a deterministic test for serving multiple reads from the retained
prefetch and a counter assertion in the concurrent test.

Co-Authored-By: Claude Fable 5 <[email protected]>
@kssenii
kssenii marked this pull request as ready for review August 7, 2026 13:55
@arsenmuk arsenmuk self-assigned this Aug 10, 2026

@arsenmuk arsenmuk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

all looks good to me. We discussed additional edge cases privately and decided that it's not applicable

@clickhouse-gh

clickhouse-gh Bot commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

No arm_release build profile data for commit 762fa93 - the build was skipped, reused from cache, or predates profile upload.

`CachedReadBufferConcurrentReadBigAtWithPrefetch` passes the real file size
into the `CachedOnDiskReadBufferFromFile` constructor, which pre-populates
`ReadBufferFromFileBase::file_size`, so `readBigAt` never reaches the lazy
`tryGetFileSize` path guarded by `file_size_mutex`.

Add `CachedReadBufferConcurrentReadBigAtUnknownFileSize`, which constructs the
buffer with size `0` (treated as unknown) and issues concurrent `readBigAt`
calls, so the resolution of `file_size` is actually exercised.

Without the fix the test fails with
`std::bad_optional_access` in `CachedOnDiskReadBufferFromFile::readBigAt`
(`file_size.value()` was read before `getFileSize` had initialized it).

Addresses #112573 (comment)
…adbigat-race' into fix-async-bounded-read-buffer-readbigat-race
@kssenii
kssenii added this pull request to the merge queue Aug 12, 2026
Merged via the queue into master with commit e9f4610 Aug 12, 2026
177 of 181 checks passed
@kssenii
kssenii deleted the fix-async-bounded-read-buffer-readbigat-race branch August 12, 2026 14:00
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 12, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

ClickGap analysis — checking which release branches are affected by the bug this PR fixes.

Reproduction
Build unit tests from a pre-fix revision (e.g. 26.7) and run:

  ninja unit_tests_dbms
  ./unit_tests_dbms --gtest_filter='AsynchronousBoundedReadBufferTest.concurrentReadBigAtWithPrefetch:FileCacheTest.CachedReadBufferConcurrentReadBigAtUnknownFileSize'

Observed without the fix: concurrent readBigAt calls with a prefetch in flight both call get() on the same prefetch_future -> std::future_error / SIGABRT; the file-cache test throws std::bad_optional_access from file_size.value() in CachedOnDiskReadBufferFromFile::readBigAt. No SQL-level repro: the path needs an object-storage read pipeline plus concurrent positioned reads, and the race is timing-dependent.

Affected versions

  • Reproduces on: 26.7, 26.6, 26.5, 26.3

  • Does NOT reproduce on: master, 25.8

  • ✓ Fix is on master — this PR's change fixes the bug on master. The releases listed above still have the bug and need the backport.

  • Caused by: Restore prefetch for small files read from object storage #108872 (commit 4a2d03b7cb18, found by bisecting in several steps)

Summary: reproduced on 26.7; caused by #108872.

Backport the fix to 26.7, 26.6, 26.5, 26.3.

cc introducing PR author: @fm4v
cc introducing PR reviewer: @kssenii

Analysis metadata

comp-filesystem-cache · Severity P2 · Finding phase_d_pr112573

@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Aug 13, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Aug 13, 2026
clickhouse-gh Bot added a commit that referenced this pull request Aug 13, 2026
Backport #112573 to 26.7: Fix async bounded read buffer readbigat race
clickhouse-gh Bot added a commit that referenced this pull request Aug 17, 2026
Backport #112573 to 26.6: Fix async bounded read buffer readbigat race
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.4-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SIGSEGV: data race on AsynchronousBoundedReadBuffer::prefetch_future (ParquetV3 prefetcher)

7 participants