mrhhsg opened a new pull request, #71289:
URL: https://github.com/apache/doris/pull/71289
### Background
For a global runtime filter, the producers of every fragment instance send
partial filters to
one merge coordinator on one BE. `merge()` accumulates them in
`GlobalMergeContext::merger`; when
the expected producer count is reached, the merged filter is serialized and
published. Serializing
a Bloom filter returns a raw pointer into the merger-owned buffer. A
recursive CTE rebuilds its
recursive-part fragments every round, and `reset_global_rf()` replaces the
merger, stage, `done`,
and the publish callback owners under `GlobalMergeContext::mtx`.
### What problem does this PR solve?
Issue Number: None
Related PR: #58916 (introduced the reset path), #61130 (added the reset
lock, left the publish outside it)
PR Type: Bugfix
Problem Summary: `merge()` released `GlobalMergeContext::mtx` before
publishing and then read the
merger, stage, `done`, targets, and callback owners unlocked. A concurrent
`reset_global_rf()`
could destroy the merger between `serialize()` and the `memcpy()` of its
Bloom buffer, an ASAN
heap-use-after-free observed once in a branch-4.2 P0 run. The publish now
captures its round's
state under the lock and sends the RPCs after the lock is released.
### Details
- **Change map:** production 164/-82 (`runtime_filter_mgr.{h,cpp}`); tests
214 lines (new BE unit
test, regression suite, generated `.out`); generated files 7 lines; docs 0.
- **Frequency:** Intermittent. One branch-4.2 P0 ASAN crash; it needs a
recursive CTE whose
recursive join publishes a global RF across an Exchange.
- **Root cause:** the unlocked publish read `merger`, `stage`, `done`,
`targetv2_info`, and
`publish_callbacks`, all of which `reset()` replaces or clears under the
same mutex; the old
merger is freed when the last reference drops, between `serialize()` and
`memcpy()`.
- **Why existing tests missed it:** the unit test only called `init()`. The
regression suite could
stay an IN set (protobuf copy, no raw pointer) and could fall back to a
bucket-shuffle plan for
small `parallel_pipeline_task_num`, which makes the RF local and skips the
global merge; P0
fuzzes that variable and `enable_runtime_filter_prune`. The suite pins
`enable_bucket_shuffle_join = false` and `enable_runtime_filter_prune =
false`, asserts
`INNER JOIN(PARTITIONED)`, and forces Bloom; a new UT pauses the publish
at a debug seam after
the state capture, runs `reset_global_rf()`, and checks the old round, the
next round, and the
direct/tree publish mode.
- **Fix:** `_prepare_rf_publish()` runs under `cnt_val.mtx`: it validates
`done`, serializes the
merged filter into an owned `IOBuf`, snapshots stage/targets, computes the
tree fanout, and
creates the per-RPC callback owners into both `publish_callbacks` (query
lifetime) and the round
state. `_send_rf_to_target()` then dispatches to the direct/tree
publishers after the lock is
released and uses only that state, so a reset cannot free the buffer or
clear the owners in use.
- **Other paths and branches:** `send_filter_size()`/`sync_filter_size()`
already synchronize with
`reset()`; the local merge path replaces the whole `LocalMergeContext` and
keeps it alive through
`shared_ptr`. The bug exists on master, branch-4.1, and branch-4.2;
branch-4.0 has no
`reset_global_rf()`.
- **Configs and defaults:** None.
### Testing
- `BUILD_TYPE=ASAN ./build.sh --be -j 64` on the final source: passed,
ASAN-instrumented.
- `./run-be-ut.sh --run
--filter='RuntimeFilterMgrTest.ResetBetweenPrepareAndPublishKeepsRoundState'`:
passed (direct and tree publish paths; the seam also asserts the publish
mode/fanout).
- Local ASAN FE + BE cluster: `rf_merge_context_reuse` passed with the
pinned partitioned plan and
the forced-Bloom case; `query_p0/runtime_filter` 11/11 passed; no ASAN
report in the BE log.
- `build-support/check-build-hygiene.sh`, `clang-format --dry-run --Werror`,
and clang-tidy on the
changed lines passed (one pre-existing diagnostic at
`runtime_filter_mgr.cpp:191`).
### Notes for Reviewers
The publish RPCs are issued without `cnt_val.mtx`; the round state keeps the
callback owners alive
while they go out, and `publish_callbacks` keeps them for the query when no
reset happens.
Pre-existing and out of scope: `debug_string()` reads the merger without the
lock (left unchanged,
fixing it would add a `_pipeline_map` shard -> `cnt_val.mtx` edge), and
`RecCTESourceOperatorX::_send_reset_global_rf` does not check
`controller.Failed()` after
`brpc::Join`.
### Release note
Fix a rare BE crash (heap-use-after-free) in recursive CTE queries that use
a global runtime
filter across an Exchange, where a round switch could free the merged Bloom
filter or reset its
publish state while it was being published.
### Check List (For Author)
- Behavior changed: No
- Does this need documentation: No
- Pre-submission status: Ready
### AI Disclosure
- Tool: Claude Code (deepseek-flash)
- Generated: Code change, unit test, regression test and its expected
output, and this message
- Human review: Pending
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]