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

   ### What problem does this PR solve?
   
   Issue Number: None
   
   Related PR: None
   
   Problem Summary: `PipelineTask::stop_if_finished()` is called by 
`FragmentMgr::cancel_worker()` (through 
`PipelineFragmentContext::clear_finished_tasks()`) on the background cancel 
thread. It copied the `_sink` shared_ptr without any lock. At the same time a 
scheduler worker can run `PipelineTask::finalize()` for the same task, which 
resets `_sink` while holding `_dependency_lifecycle_lock`. Copying and 
resetting the same shared_ptr instance from two threads without synchronization 
is a data race (undefined behavior): the cancel thread can observe a torn 
pointer/control block and then call `is_finished()` on an object that is being 
released. Every other cross-thread reader of `_sink` (`debug_string()`, the 
hybrid scheduler submit path) is already synchronized; `stop_if_finished()` was 
the only one left.
   
   Fix: take `_dependency_lifecycle_lock` in `stop_if_finished()`, return if 
the task is already finalized, and copy `_sink` under the lock, following the 
pattern of `debug_string()`. `is_finished()` and `unblock_all_dependencies()` 
are called after the lock is released, because `unblock_all_dependencies()` 
acquires the same non-recursive mutex. A debug point inside the critical 
section lets the unit test hold the lock deterministically.
   
   ### Release note
   
   None
   
   ### Check List (For Author)
   
   - Test: Unit Test
       - Added `PipelineTaskTest.TEST_STOP_IF_FINISHED_EXCLUDES_FINALIZE`: it 
holds `stop_if_finished()` inside the critical section and checks that a 
concurrent `finalize()` is blocked and cannot reset `_sink`, and that a 
finalized task is skipped. It fails without the lock and passes with it (200 
repeated runs). All `PipelineTaskTest` cases pass.
       - No regression test: the race window is a few instructions wide and 
cannot be triggered reliably from SQL.
   - Behavior changed: No
   - Does this need documentation: No


-- 
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