mrhhsg opened a new pull request, #71307:
URL: https://github.com/apache/doris/pull/71307

   ### Background
   
   For a global runtime filter, the producers of every fragment instance send 
their 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: #71289 (master fix), #61806 (landed on branch-4.1; branch-4.2 
inherits the recursive CTE reset path through the shared lineage), #58916 and 
#61130 (master history of the reset path)
   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, the ASAN
   heap-use-after-free observed in a branch-4.2 P0 run; the other reads were 
data races. This is the
   branch-4.2 backport of the master fix; the code change is identical. The 
publish now runs under
   the same lock that the reset takes.
   
   ### Details
   
   - **Frequency:** Intermittent. One branch-4.2 P0 ASAN crash 
(`heap-use-after-free`, READ of size
     1048576); 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()`, and 
the regression suite
     could stay an IN set (protobuf copy, no raw pointer) or 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 `runtime_filter_type = 2` so the 
raw-pointer Bloom path
     runs on every round.
   - **Fix:** `merge()` calls `_send_rf_to_target()` while still holding 
`cnt_val.mtx`: the
     readiness check, the merge, the serialization and copy of the Bloom 
buffer, and the publish
     state (`done`, `stage`, `publish_callbacks`) are atomic with respect to 
`reset()` under the
     same lock. `send_filter_size()` already holds this lock across its RPCs, 
so the scope is
     consistent. A round reset may wait for the publish to be issued (serialize 
+ copy + async RPC
     submission); that is bounded and happens once per round.
   - **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 branch-4.2: passed, 
ASAN-instrumented
     (`__asan_init` present); glibc 2.17 compatibility check passed. The FE 
used for the local
     cluster was built with `MVN_OPT=-Dmaven.test.skip=true ./build.sh --fe` 
because branch-4.2 HEAD
     has a pre-existing unrelated test-source compile error
     (`GroupCommitManagerBackendSelectionTest`); this PR changes no FE source.
   - `./run-be-ut.sh --run --filter='RuntimeFilterMgrTest.*'`: passed (5/5, 257 
ms).
   - Local ASAN FE + BE cluster (branch-4.2 binaries): 
`recursive_cte/rf_merge_context_reuse` passed
     with the pinned partitioned plan and the forced-Bloom case; the 
`query_p0/join` runtime-filter
     suites (`test_runtime_filter_boolean`, `test_runtime_filter_decimal256`) 
and
     `query_p0/system/test_runtime_filter` (after the standard `query_p0/load`) 
passed; the BE
     stayed alive and logged no ASAN report.
   - `clang-format --dry-run --Werror` passed on the changed C++ file; 
clang-tidy reports only the
     pre-existing diagnostics at `runtime_filter_mgr.cpp:59`, `:203`, and 
`:628`, none on the changed
     lines. branch-4.2 has no `build-support/check-build-hygiene.sh` gate.
   
   ### Notes for Reviewers
   
   The publish (including the asynchronous RPC submission) now runs under 
`cnt_val.mtx`; the reset
   RPC waits for that submission but never for RPC completion. Direct and tree 
publish keep their
   existing behavior. Pre-existing and out of scope: `debug_string()` reads the 
merger without the
   lock, and `_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: Branch-4.2 backport commit, 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]

Reply via email to