This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch branch-4.1
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-4.1 by this push:
     new 98f95869fcd branch-4.1: [fix](be) Avoid scan executor shutdown use 
after free #65220 (#66217)
98f95869fcd is described below

commit 98f95869fcdd20b6af9efa2d11dba41b78596af0
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Mon Aug 3 10:28:51 2026 +0800

    branch-4.1: [fix](be) Avoid scan executor shutdown use after free #65220 
(#66217)
    
    Cherry-picked from #65220
    
    Co-authored-by: TengJianPing <[email protected]>
---
 be/src/exec/scan/scanner_context.cpp | 14 ++++++++------
 be/src/exec/scan/scanner_context.h   |  1 +
 be/src/exec/scan/scanner_scheduler.h |  8 ++++++--
 3 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/be/src/exec/scan/scanner_context.cpp 
b/be/src/exec/scan/scanner_context.cpp
index 3371af5bf8e..279040c5168 100644
--- a/be/src/exec/scan/scanner_context.cpp
+++ b/be/src/exec/scan/scanner_context.cpp
@@ -40,6 +40,7 @@
 #include "exec/operator/scan_operator.h"
 #include "exec/scan/scan_node.h"
 #include "exec/scan/scanner_scheduler.h"
+#include "exec/scan/task_executor/task_executor.h"
 #include "runtime/descriptors.h"
 #include "runtime/exec_env.h"
 #include "runtime/runtime_profile.h"
@@ -123,6 +124,7 @@ Status ScannerContext::init() {
     if (auto* task_executor_scheduler =
                 
dynamic_cast<TaskExecutorSimplifiedScanScheduler*>(_scanner_scheduler)) {
         std::shared_ptr<TaskExecutor> task_executor = 
task_executor_scheduler->task_executor();
+        _task_executor = task_executor;
         TaskId task_id(fmt::format("{}-{}", print_id(_state->query_id()), 
ctx_id));
         _task_handle = DORIS_TRY(task_executor->create_task(
                 task_id, []() { return 0.0; },
@@ -187,11 +189,11 @@ ScannerContext::~ScannerContext() {
     block.reset();
     DorisMetrics::instance()->scanner_ctx_cnt->increment(-1);
     if (_task_handle) {
-        if (auto* task_executor_scheduler =
-                    
dynamic_cast<TaskExecutorSimplifiedScanScheduler*>(_scanner_scheduler)) {
-            
static_cast<void>(task_executor_scheduler->task_executor()->remove_task(_task_handle));
+        if (auto task_executor = _task_executor.lock()) {
+            static_cast<void>(task_executor->remove_task(_task_handle));
         }
         _task_handle = nullptr;
+        _task_executor.reset();
     }
 }
 
@@ -389,11 +391,11 @@ void ScannerContext::stop_scanners(RuntimeState* state) {
     }
     _tasks_queue.clear();
     if (_task_handle) {
-        if (auto* task_executor_scheduler =
-                    
dynamic_cast<TaskExecutorSimplifiedScanScheduler*>(_scanner_scheduler)) {
-            
static_cast<void>(task_executor_scheduler->task_executor()->remove_task(_task_handle));
+        if (auto task_executor = _task_executor.lock()) {
+            static_cast<void>(task_executor->remove_task(_task_handle));
         }
         _task_handle = nullptr;
+        _task_executor.reset();
     }
     // TODO yiguolei, call mark close to scanners
     if (state->enable_profile()) {
diff --git a/be/src/exec/scan/scanner_context.h 
b/be/src/exec/scan/scanner_context.h
index 857dfb736e9..ba1c3235660 100644
--- a/be/src/exec/scan/scanner_context.h
+++ b/be/src/exec/scan/scanner_context.h
@@ -239,6 +239,7 @@ protected:
     std::shared_ptr<ResourceContext> _resource_ctx;
     std::shared_ptr<Dependency> _dependency = nullptr;
     std::shared_ptr<doris::TaskHandle> _task_handle;
+    std::weak_ptr<doris::TaskExecutor> _task_executor;
 
     std::atomic<int64_t> _block_memory_usage = 0;
 
diff --git a/be/src/exec/scan/scanner_scheduler.h 
b/be/src/exec/scan/scanner_scheduler.h
index f1939409bda..66d5fd55f65 100644
--- a/be/src/exec/scan/scanner_scheduler.h
+++ b/be/src/exec/scan/scanner_scheduler.h
@@ -351,10 +351,14 @@ public:
                             : std::max(48, CpuInfo::num_cores() * 2),
                     std::chrono::milliseconds(100), std::nullopt));
 
-            auto wrapped_scan_func = [this, task_handle, scan_func = 
scan_task.scan_func]() {
+            std::weak_ptr<TaskExecutor> task_executor = _task_executor;
+            auto wrapped_scan_func = [task_executor, task_handle,
+                                      scan_func = scan_task.scan_func]() {
                 bool result = scan_func();
                 if (result) {
-                    
static_cast<void>(_task_executor->remove_task(task_handle));
+                    if (auto executor = task_executor.lock()) {
+                        static_cast<void>(executor->remove_task(task_handle));
+                    }
                 }
                 return result;
             };


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to