Skip to content

Cherry pick #114668 to 25.8: Stop Parquet background reads before releasing the format's read buffer - #114874

Merged
robot-ch-test-poll3 merged 7 commits into
backport/25.8/114668from
cherrypick/25.8/114668
Aug 15, 2026
Merged

robot-ch-test-poll3 merged 7 commits into
backport/25.8/114668from
cherrypick/25.8/114668

Conversation

@robot-clickhouse

Copy link
Copy Markdown
Member

Original pull-request #114668

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 6 commits August 13, 2026 07:29
A dictionary that reads Parquet from a local file hands its ReadBuffer to the
input format via IInputFormat::addBuffer(), so the format owns it in
owned_buffers. When the pipeline finishes, ISource::work() calls onFinish() on
both the clean (ISource.cpp:133) and the exception (ISource.cpp:140) path, which
reaches IInputFormat::resetReadBuffer() and clears owned_buffers, destroying the
buffer.

ParquetV3BlockInputFormat did not override that hook, and Parquet::Prefetcher
holds a non-owning SeekableReadBuffer* to the same buffer (Prefetcher.h:175).
Background prefetch and decode tasks therefore kept reading through a destroyed
object. Prefetcher.cpp:111 is a virtual call on it, so the 8-byte read is the
freed vptr; observed both as an AddressSanitizer heap-use-after-free in
Prefetcher::readSync and as a libc++abi pure-virtual abort. ~Prefetcher() does
perform the correct shutdown handshake, but only at format destruction, which is
strictly later than onFinish(); the window between the two is the bug.

The hook is resetReadBuffer() rather than onFinish() because it is what actually
frees the buffers and it has a second caller,
StreamingFormatExecutor::execute()'s SCOPE_EXIT. Two task families need
draining, not one: Prefetcher's IO tasks and ReadManager's decode tasks, which
re-enter readSync inline via getRangeData(). ReadManager is drained first, since
it produces the tasks that reach the prefetcher. The reader itself is kept
alive, because getMatchedBuckets() reads row group metadata after the source is
exhausted to populate the query condition cache.

This mirrors ParallelParsingInputFormat::onFinish(), which waits for its threads
for the same reason.

Found by the La Casa Del Dolor (arm_asan_ubsan) fuzzer check. Reachable without
a fuzzer: ReadBufferFromFile leaves use_pread false, so a plain local file takes
the SeekAndRead path, and any CREATE DICTIONARY ... SOURCE(FILE(... format
'Parquet')) whose load throws hits it.

Co-Authored-By: Claude Fable 5 <[email protected]>
The regression test passed on the unfixed binary most of the time because it
did not exercise what its comments claimed.

Two independent defects, both confirmed by measurement before being changed.
The five-iteration loop performed a single real load: once the dictionary is
FAILED, a non-forced dictGet cannot re-enter startLoading, because
getMinIDToFinishLoading(false) returns 1 while loading_id is already at least
1, so checkLoaded rethrows the stored exception. Nothing filled the gap inside
one run, since a failed load pins the next update five seconds out and the
periodic updater's own period is also five seconds. Counting error messages
could not see this, as the replayed message is byte-identical. Per-iteration
prefetcher reads were 93, 0, 0, 0, 0; with SYSTEM RELOAD DICTIONARY, which
forces a reload, they are 85, 90, 85, 86, 83.

The three race-widening settings never reached the load either. A dictionary
loads in the global context, and the file source only applies the DDL SETTINGS
clause, so the format was built from defaults. Moving min_bytes_for_seek into
that clause changes the per-reload read count from 87 to 241 at 1 and to 2 at
100000000, while the same value on the client command line does not change it
at all. A typo'd name inside the clause is silently ignored, which is the
negative control that a dead clause cannot look like a live one.

With both fixed the test still reddened rarely, so the remaining difference
against the standalone reproducer was isolated: at 2000000 rows it survives
five forced reloads with 8 threads and dies with 32, so concurrency carries
the window, not fixture size. The test now reddens 7 of 7 scored runs on the
unfixed binary, all of them a pure-virtual abort at Prefetcher.cpp:111, and
passes 11 of 11 fixed plus 50 of 50 randomized runs. A local unfixed run also
produced a full heap-use-after-free whose symbolized stack matches the CI
report frame for frame, including both thread names and the 272-byte region.

The loop is now guarded by a control that reads per-query ProfileEvents from
system.query_log, so each of the five reloads is proven to have reached the
reader. The global system.events counter was rejected as pollutable on a
shared server, and system.dictionaries.error_count as unreliable, since the
periodic updater also increments it.

The long tag is dropped because it costs 90 percent of the flaky check's
repeats, and the test measures 16 to 24 seconds against a 180 second
threshold. The only source change is a comment trim.

Co-Authored-By: Claude Fable 5 <[email protected]>
The test asserted ParquetPrefetcherReadSeekAndRead > 0 per reload, but that
counter is also incremented by the two synchronous footer reads in
Reader::readFileMetaData, so a load rejected while its footer was parsed
satisfied the assertion without ever reading a row group. Measured on a file
with a corrupt footer length: the old assertion passed 5 of 5 while no row
group was read. ParquetReadRowGroups is counted once row groups are being
read, and measured 397 on 20 of 20 reloads of this fixture, so it separates
the two cases without making the test timing dependent.

Pin input_format_parquet_enable_row_group_prefetch so the prefetch pool
exists regardless of a server side or future default: without it
Prefetcher::scheduleTask does nothing and getRangeData runs the read inline
on the decoding thread, which no read counter distinguishes.

Renumber to 04895, since 04885 was taken first by another unmerged test.
Content half of the previous commit, which recorded only the rename.
The style check rejects a bare global SYSTEM FLUSH LOGS
(ci/jobs/scripts/check_style/various_checks.sh:192): flushing every log table
on the server disturbs tests running in parallel. The test only reads
system.query_log, so name that table.

Verified the scoped form is effective rather than silently ignored: the log
name is parsed and validated, so a wrong name errors with BAD_ARGUMENTS, and
with the flush removed entirely the reloads_that_read oracle reads 0 instead
of 5, i.e. the flush is load-bearing for the assertion.
…r-lifetime

Stop Parquet background reads before releasing the format's read buffer
@robot-clickhouse robot-clickhouse added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-critical-bugfix labels Aug 14, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

The four 26.x backports of #114668 apply cleanly and are non-vacuous; 25.8 cannot take this change as-is. Measured against the current branch tips.

Simulating the cherry-pick per branch (git merge-tree --merge-base=<merge>^1 <tip> <merge>): 26.3, 26.5, 26.6 and 26.7 all resolve with 0 conflicts, and in each resulting tree Prefetcher::shutdownTasks, ReadManager::shutdownTasks and the ParquetV3BlockInputFormat::resetReadBuffer override are present with the same counts as master (pre-fix control: 0). So #114875 through #114878 need nothing from me beyond CI, which is running now.

25.8 conflicts in Parquet/ReadManager.h, and resolving that conflict is not enough. The fix's resetReadBuffer body takes std::lock_guard lock(reader_mutex), and 25.8 has no reader_mutex: it arrived with 0f37dfd "Fix UBSAN error in ParquetV3BlockInputFormat::onCancel", which is an ancestor of 26.3, 26.5, 26.6 and 26.7 but not of 25.8. A forced resolution therefore compiles nowhere: in the merged 25.8 tree the member is used once and declared zero times. Cherry-picking that prerequisite first does not clear the path either, it conflicts on its own in ParquetV3BlockInputFormat.cpp.

There is also a reachability difference. On 25.8 input_format_parquet_use_native_reader_v3 is EXPERIMENTAL and defaults to false, so the affected reader is opt-in there, while it is the default on 26.3 and obsolete-always-on from 26.5. On top of that, 25.8 increments ParquetReadRowGroups only in the old Arrow reader, not in Parquet/ReadManager.cpp, so the reloads_that_read guard in 04895_parquet_dictionary_source_prefetch_lifetime.sh would report 0 and the test would assert nothing on that branch.

I have not touched the cherry-pick branch: per @ pufit's standing instruction I do not hand-resolve release-branch ports. @PedroTadim you applied the backport label, so this is your call. My suggestion is to close #114874 and leave 25.8 unfixed, since the reader is experimental and off by default there. If you do want 25.8 covered, it needs a hand-adapted variant plus its own test guard rather than this cherry-pick, and I can write that if you ask.

Test number 04895 is free on all five branches.

…ypick/25.8/114668

# Conflicts:
#	src/Processors/Formats/Impl/Parquet/ReadManager.h
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Resolved the conflict with a hand-adapted 25.8 variant. The merge adds the missing reader synchronization around initialization, cancellation, parser reset, and read-buffer reset; it stops ReadManager and Prefetcher tasks before the input buffer is released. The regression test now explicitly enables the experimental V3 reader and omits the unavailable ParquetReadRowGroups counter guard.

The branch is pushed at 6ebcfd2f1ef3. Local compilation could not run because CMake rejected this host CPU before compiling (cmake/cpu_features.cmake:110); CI will compile and run the test.

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

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants