Repository navigation
Conversation
getInfoForLog() read state->buf->available(), offset() and buffer().size(), which read the working_buffer char* pointers non-atomically. state->buf can be a remote reader shared between file segments (getRemoteReadBuffer), and this logging runs after the buffer was released, while another thread already reads through the shared reader and swaps those pointers via SwapHelper in BoundedReadBuffer::nextImpl(). That is a data race. This is the same race PR ClickHouse#55372 fixed for getFileOffsetOfBufferEnd() (made it atomic) but the three working_buffer accessors were left unguarded. Log only getFileOffsetOfBufferEnd() here, which is atomic. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-1:20260703-194000 |
|
cc @kssenii @antonio2368 for review. This is a follow-up to #55372, which fixed the same |
|
Workflow [PR], commit [f5196b6] Summary: ❌
AI ReviewSummaryThis PR removes the racy working-buffer reads from Final Verdict
|
CachedOnDiskReadBufferFromFile::getInfoForLog reads getFileOffsetOfBufferEnd() of a remote reader that getRemoteReadBuffer shares across file segments, from a thread logging the read info while another thread reads through the same buffer. ReadBufferFromAzureBlobStorage returned a plain off_t offset there, mutated by nextImpl/seek, so on Azure-backed filesystem caches that read still raced. ReadBufferFromS3 and ReadBufferFromWebServer already make offset atomic for exactly this reason. Mirror that: make the Azure reader's offset a std::atomic<off_t> and read it via .load() in getFileOffsetOfBufferEnd() and the few logging sites. Completes the getInfoForLog TSAN race fix for the Azure path. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Updated pre-PR validation gate after extending the fix to the Azure path (f5196b6, per the review above): Pre-PR validation gate (Azure atomic offset addition) (click to expand)
Session id: cron:clickhouse-worker-slot-7:20260703-212900 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 11/16 (68.75%) · Uncovered code |
CI finish ledger — f5196b6Every failure below has an owner. This PR touches only
Reports:
Session id: cron:our-pr-ci-monitor:20260704-023000 |
|
Fixing PR for the |
| /// this variable via getFileOffsetOfBufferEnd() from a thread that logs the | ||
| /// read info (getInfoForLog) while another thread reads through this shared | ||
| /// reader. Same reason ReadBufferFromS3/ReadBufferFromWebServer use atomic. | ||
| std::atomic<off_t> offset = 0; |
There was a problem hiding this comment.
Is this change still needed after the change in src/Disks/IO/CachedOnDiskReadBufferFromFile.cpp? AFAIS it is not
There was a problem hiding this comment.
Yes, it is still needed. The .cpp change dropped the three working_buffer accessors but kept state->buf->getFileOffsetOfBufferEnd(), which is the racy read on the Azure path.
Call path:
getInfoForLog()readsstate->buf->getFileOffsetOfBufferEnd().state->bufcomes fromgetRemoteReadBuffer->create_remote_read_buffer: whenimpl->supportsRightBoundedReads()is true it returns the impl un-wrapped (noBoundedReadBuffer).ReadBufferFromAzureBlobStorage::supportsRightBoundedReads()returnstrue, sostate->bufis the raw Azure reader.- That reader is shared across file segments (
setRemoteFileReader/getRemoteFileReader/info.remote_file_reader), so the logging thread and the reading thread can touch it at once. - Azure's
getFileOffsetOfBufferEnd()returned a plainoff_t offsetwritten bynextImpl()(offset += bytes_read) andseek()(offset = offset_) on the reading thread, so that read still races.
ReadBufferFromS3 and ReadBufferFromWebServer already make offset a std::atomic<off_t> for exactly this reason; Azure was the last remote reader left non-atomic. Making the Azure offset atomic completes the fix for the Azure-backed cache path.
|
@groeneai investigate why this issue does not happen without BoundedReadBuffer, for example there is AsynchronousBoundedReadBuffer -- why it never hit this issue? |
|
The race is not about
So the shared-un-wrapped-remote-reader case is unique to the |
|
Closing in favour of #110154 |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a data race in the filesystem cache read path.
CachedOnDiskReadBufferFromFile::getInfoForLog()read the working buffer pointers (available,offset,buffer size) of a remote reader that can be shared between file segments and used concurrently by another thread, which could race with the buffer swap inBoundedReadBuffer.Description
TSAN report from
BuzzHouse (amd_tsan):Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108838&sha=2586502fbedc5ab52b2531d82464fb0251bac909&name_0=PR&name_1=BuzzHouse%20%28amd_tsan%29
BufferBase::Buffer::end()<-available()<-CachedOnDiskReadBufferFromFile::getInfoForLog()(src/Disks/IO/CachedOnDiskReadBufferFromFile.cpp).std::swap<char*><-BufferBase::Buffer::swap()<-SwapHelper::~SwapHelper()<-BoundedReadBuffer::nextImpl().Root cause:
getInfoForLog()loggedstate->buf->available(),->offset()and->buffer().size(), all of which read theworking_bufferchar*pointers non-atomically.state->bufcan be a remote reader (BoundedReadBuffer) that is deliberately shared between file segments (seegetRemoteReadBuffer), and this logging runs after the buffer is released, while another thread already reads through the shared reader and swaps those pointers viaSwapHelperinBoundedReadBuffer::nextImpl().This is the same class of race that PR #55372 fixed for
getFileOffsetOfBufferEnd()by making it atomic and documenting the contract (the buffer is released before logging, so another thread can already use it). The three remaining working-buffer accessors were left unguarded. This change logs onlygetFileOffsetOfBufferEnd()here, which is atomic, and leaves a NOTE explaining why the other accessors must not be read.