matanper opened a new issue, #70842:
URL: https://github.com/apache/doris/issues/70842

   ### Search before asking
   
   - [x] I had searched in the 
[issues](https://github.com/apache/doris/issues?q=is%3Aissue) and found no 
similar issues.
   
   ### Version
   
   master (and branch-4.1 since #67548), `be/src/runtime/query_context.cpp`.
   
   ### What's Wrong?
   
   Since #67344 (branch-4.1: #67548), `QueryContext::cancel()` is the only 
cancellation entry point, and `PipelineFragmentContext::cancel()` runs only 
when `QueryContext::cancel()` calls it. `QueryContext::cancel()` works in this 
order:
   
   1. `_exec_status.update(new_status)`: the first error wins, and every later 
call returns at once.
   2. It copies the fragments in `_fragment_id_to_pipeline_ctx` under 
`_pipeline_map_write_lock` (`query_context.cpp:363-369` on master).
   3. It cancels each copied fragment.
   
   `QueryContext::set_pipeline_context()` (`query_context.cpp:455-461`) only 
inserts the new fragment. It is called after the fragment is prepared:
   - `fragment_mgr.cpp:714` for a new fragment;
   - `fragment_mgr.cpp:1384` for the fragment a recursive CTE rebuilds between 
rounds.
   
   Suppose the query is cancelled after a fragment is prepared but before it is 
registered. The copy in step 2 does not include that fragment, so it is never 
cancelled.
   
   Its tasks then close with the query's error and call 
`ctx->get_query_ctx()->cancel(exec_status)` (`task_scheduler.cpp:84`). That is 
now a no-op, because the query status is already set. So nothing ever calls 
`PipelineFragmentContext::cancel()` for the fragment:
   - `notify_close()` is never invoked;
   - a fragment with `need_notify_close` keeps it;
   - the fragment is never removed from the fragment manager, and it keeps its 
`QueryContext` alive.
   
   Before #67344, every task that closed with an error called 
`PipelineFragmentContext::cancel()` itself, which covered this case.
   
   ### What You Expected?
   
   A fragment registered after its query was cancelled is cancelled as well, 
and is released like every other fragment of the query.
   
   ### How to Reproduce?
   
   The window is between `prepare()` and `set_pipeline_context()`, which is too 
short to hit reliably by hand. It can be forced with a debug point between the 
two calls that cancels the query, for example a cancel issued from the FE while 
the debug point sleeps. Then check that the fragment's tasks close with the 
query's error and that the fragment is never removed. The recursive-CTE rebuild 
path (`fragment_mgr.cpp:1384`) is the easiest place to hit it, because every 
round re-registers a new fragment context.
   
   ### Anything Else?
   
   **Suggested fix.** After inserting the fragment in `set_pipeline_context()`, 
if `_exec_status` is already an error, cancel the fragment it just registered 
with that status. Cancellation records the status before it copies the map, so 
either the copy includes the new fragment, or the registering thread sees the 
status and cancels the fragment itself. `PipelineFragmentContext` needs 
`QueryContext::set_pipeline_context` as a second friend next to 
`QueryContext::cancel`. We have a patch and can open a PR.
   
   cc @yiguolei (author of #67344)
   
   ### Are you willing to submit PR?
   
   - [x] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   


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