Repository navigation
Stop Parquet background reads before releasing the format's read buffer - #114668
alexey-milovidov merged 5 commits into
Conversation
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.
Internal second-model reviewThree rounds of independent cold review, each followed by a second-model gate pass. The source
Two residuals were adjudicated as not blocking, and are recorded here rather than dropped:
Verified sound with no change required: |
|
Workflow [PR], commit [64e43f8] Summary: ✅
AI ReviewSummaryThis PR fixes a real lifetime bug in Final Verdict✅ Approved. No new findings. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 14/14 (100.00%) · Uncovered code |
Pre-PR validation gate
50 randomized runs of the new test: 50 passed, 0 skipped, 1267.73 s, no sanitizer report and no pure-virtual abort in the server log. Every arm asserts the server's |
|
cc @al13n321, could you review this? |
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.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units18 translation units recompiled, 187 s compile time in total, 18 of them have a recent master baseline. |
CI finish ledger - 64e43f8CI is fully finished on this commit with no failing checks, so there is nothing to own.
Session id: cron:our-pr-ci-monitor:20260813-233000 |
…ing the format's read buffer
Backport #114668 to 26.7: Stop Parquet background reads before releasing the format's read buffer
Cherry pick #114668 to 25.8: Stop Parquet background reads before releasing the format's read buffer
…ing the format's read buffer
Backport #114668 to 26.5: Stop Parquet background reads before releasing the format's read buffer
Backport #114668 to 26.3: Stop Parquet background reads before releasing the format's read buffer
Backport #114668 to 26.6: Stop Parquet background reads before releasing the format's read buffer
Backport #114668 to 25.8: Stop Parquet background reads before releasing the format's read buffer
…ore releasing the format's read buffer
…ore releasing the format's read buffer
…ore releasing the format's read buffer
PROTO: Parquet through a format that owns its buffer (e.g. a FILE dictionary) -> crash. Effect in the production build a394e92: its upstream test(s) 04895_parquet_dictionary_source_prefetch_lifetime fail on it (the server process died in 04895_parquet_dictionary_source_prefetch_lifetime) and pass on official 26.8.8.8, run on isolated servers (evidence/fix_ci_20260925/upstream_repro/server_triage_PROD263_vs_UP2688.tsv). Upstream patch unchanged, with its test(s): 04895_parquet_dictionary_source_prefetch_lifetime. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed heap memory corruption when reading Parquet through an input format that owns its read buffer, for example a dictionary with
SOURCE(FILE(... format 'Parquet')). Background prefetch and decode tasks could still read and write through the buffer after the pipeline released it, which could abort the server.Description
Closes #114612, reported by @ PedroTadim, who asked me to go ahead in #114612 (comment).
A
ReadBufferhanded to an input format viaaddBuffer()lives in the format'sowned_buffers.ISource::work()callsonFinish()on both the clean and the exception path, reachingIInputFormat::resetReadBuffer(), which clearsowned_buffersand destroys the buffer.ParquetV3BlockInputFormatdid not override that hook, andParquet::Prefetcherholds a non-owningSeekableReadBuffer *to the same buffer, so background tasks kept using a destroyed object. It is not only a bad read: the report on the issue is a 1 MiB write into freed heap throughReadBuffer::next(), silent on a release build.~Prefetcher()does the right handshake, but only at format destruction, later thanonFinish(); that window is the bug.The fix overrides
resetReadBuffer()to drain background tasks before the base class releases the buffers, mirroringParallelParsingInputFormat::onFinish(). Three details are forced by the surrounding code: the hook isresetReadBuffer(), notonFinish(), since it frees the buffers and has a second caller inStreamingFormatExecutor; the reader is drained but kept alive, becausegetMatchedBuckets()reads row group metadata after exhaustion; andReadManageris drained beforePrefetcher, because decode tasks re-enterreadSyncinline viagetRangeData().Sibling teardown paths:
resetParser()already destroys the reader before delegating,onCancel()cancels it, and the reuse route throughStreamingFormatExecutorcallsresetParser()on every exit, so a drained reader is never reused.No fuzzer needed:
ReadBufferFromFileleavesuse_preadfalse, so a local file takesSeekAndRead, and anyCREATE DICTIONARY ... SOURCE(FILE(... format 'Parquet'))whose load throws reaches it.Validated on ASAN: the added test fails 7/7 unfixed, passes 11/11 fixed; a standalone reproducer 31/33 unfixed, 0/35 fixed; a negative control with only the override reverted reddens at the unfixed rate. Parquet and polygon-dictionary suites gave an identical failing set on both binaries (187 each); 50 randomized-settings repeats passed clean.
Version info
26.7.4.42,26.6.3.56,26.5.7.56,26.3.18.30,25.8.31.6