Skip to content

Fix data race in CachedOnDiskReadBufferFromFile::getInfoForLog - #110565

Closed
alexey-milovidov wants to merge 2 commits into
masterfrom
fix-cache-getinfoforlog-race
Closed

alexey-milovidov wants to merge 2 commits into
masterfrom
fix-cache-getinfoforlog-race

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

Closes: #110564
Related: #109203

CachedOnDiskReadBufferFromFile::getInfoForLog logged the internal state of state->buf (its buffer pointers and offsets: available, offset, buffer().size and getFileOffsetOfBufferEnd). For the download read type (REMOTE_FS_READ_AND_PUT_IN_CACHE) the implementation buffer is a remote file reader shared via FileSegment::getRemoteFileReader, which hands it out without extracting it so that an intersecting read on another thread can reuse it. Since getInfoForLog is also called from the nextImplStep scope guard after the reader has been handed off — from a thread that no longer owns the read — those reads raced with the current downloader mutating the same reader in BufferBase::set.

ThreadSanitizer reported this as a data race (write in BufferBase::set from ReadBufferFromS3::nextImpl, previous read in BufferBase::offset from getInfoForLog) under the Stateless tests (amd_tsan, s3 storage, sequential) job. It reproduced on several unrelated pull requests, so it is a pre-existing race in master.

This change stops getInfoForLog from reading the shared reader's internal state; it now logs only whether an implementation buffer is present, which is safe to read from any thread. There is no deterministic way to add a regression test for a timing-dependent race of this kind; the fix is covered by the existing amd_tsan (s3 storage) runs.

Example CI report where it was observed: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110433&sha=d88ed3ff78a877cee82661cfad68267ffb415a76&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20s3%20storage%2C%20sequential%2C%202%2F2%29

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)

🤖 Generated with Claude Code

`getInfoForLog` logged the internal state of `state->buf` (its buffer
pointers and offsets: `available`, `offset`, `buffer().size` and
`getFileOffsetOfBufferEnd`). For the download read type the implementation
buffer is a remote file reader shared via `FileSegment::getRemoteFileReader`,
which hands it out without extracting it so that an intersecting read on
another thread can reuse it. `getInfoForLog` is also invoked from the
`nextImplStep` scope guard after the reader has been handed off - i.e. from
a thread that no longer owns the read - so reading those fields raced with
the current downloader mutating them in `BufferBase::set`.

The race was reported by ThreadSanitizer under the `Stateless tests
(amd_tsan, s3 storage, sequential)` job (write in `BufferBase::set` from
`ReadBufferFromS3::nextImpl`, previous read in `BufferBase::offset` from
`getInfoForLog`), and reproduced on several unrelated pull requests.

Stop logging the buffer's internal state; log only whether an
implementation buffer is present, which is safe to read from any thread.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110433&sha=d88ed3ff78a877cee82661cfad68267ffb415a76&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20s3%20storage%2C%20sequential%2C%202%2F2%29
Observed on: #110433

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

clickhouse-gh Bot commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [90d74c7]

Summary: ✅


AI Review

Summary

This PR narrows CachedOnDiskReadBufferFromFile::getInfoForLog so it no longer reads buffer internals after downloader ownership can move to another thread. I traced the shared-reader handoff through getRemoteReadBuffer, nextImplStep, and FileSegment state transitions, and I did not find a remaining correctness issue in the current patch.

Final Verdict

✅ No blocking or major findings.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jul 15, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

The red on Stateless tests (amd_asan_ubsan, distributed plan, parallel) (unrelated MEMORY_LIMIT_EXCEEDED/Code-241 failures across random test names, e.g. 03634_autopr_output_bytes_estimation, 03630_join_blocks_with_different_constness) is the known distributed-plan sanitizer OOM infra issue tracked at #110574, not caused by this change. Merged master to get a fresh CI run in the meantime.

@clickhouse-gh

clickhouse-gh Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.90% 85.90% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 78.10% 78.10% +0.00%

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

Full report · Diff report

@alexey-milovidov

Copy link
Copy Markdown
Member Author

Closing: this is superseded by #110154 (merged into master on 2026-07-16, merge commit befb96d), which fixes the same data race at the root — it releases state (and with it the reference to the segment's shared remote reader) before completing the segment and re-electing a downloader, so getInfoForLog never touches a reader owned by another thread. That approach also preserves the detailed buf.available/buf.offset/buf.size/buf.buffer_end_offset diagnostics, which this pull request would have removed, and adds unit-test coverage for the exception-path ownership handoffs. With #110154 merged, this change is redundant and its comment about the race would no longer be accurate.

Related: #110154

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Data race in CachedOnDiskReadBufferFromFile::getInfoForLog reading a shared remote reader (amd_tsan, s3 storage)

1 participant