diff --git a/be/src/pipeline/exec/spill_utils.h b/be/src/pipeline/exec/spill_utils.h index 635a6a6bbbcf8a..925e7df44e607e 100644 --- a/be/src/pipeline/exec/spill_utils.h +++ b/be/src/pipeline/exec/spill_utils.h @@ -40,17 +40,18 @@ class SpillRunnable : public Runnable { ~SpillRunnable() override = default; void run() override { + // Should lock task context before scope task, because the _state maybe + // destroyed when run is called. + auto task_context_holder = _task_context_holder.lock(); + if (!task_context_holder) { + return; + } SCOPED_ATTACH_TASK(_state); Defer defer([&] { std::function tmp; std::swap(tmp, _func); }); - auto task_context_holder = _task_context_holder.lock(); - if (!task_context_holder) { - return; - } - auto shared_state_holder = _shared_state_holder.lock(); if (!shared_state_holder) { return;