diff --git a/be/src/vec/sink/writer/vtablet_writer.cpp b/be/src/vec/sink/writer/vtablet_writer.cpp index 1e888f2460e60c..97e8ffe1e77fd2 100644 --- a/be/src/vec/sink/writer/vtablet_writer.cpp +++ b/be/src/vec/sink/writer/vtablet_writer.cpp @@ -357,9 +357,10 @@ Status VNodeChannel::init(RuntimeState* state) { _cur_add_block_request->set_eos(false); // add block closure + // Has to using value to capture _task_exec_ctx because tablet writer may destroyed during callback. _send_block_callback = WriteBlockCallback::create_shared(); - _send_block_callback->addFailedHandler([this](bool is_last_rpc) { - auto ctx_lock = _task_exec_ctx.lock(); + _send_block_callback->addFailedHandler([&, task_exec_ctx = _task_exec_ctx](bool is_last_rpc) { + auto ctx_lock = task_exec_ctx.lock(); if (ctx_lock == nullptr) { return; } @@ -367,8 +368,9 @@ Status VNodeChannel::init(RuntimeState* state) { }); _send_block_callback->addSuccessHandler( - [this](const PTabletWriterAddBlockResult& result, bool is_last_rpc) { - auto ctx_lock = _task_exec_ctx.lock(); + [&, task_exec_ctx = _task_exec_ctx](const PTabletWriterAddBlockResult& result, + bool is_last_rpc) { + auto ctx_lock = task_exec_ctx.lock(); if (ctx_lock == nullptr) { return; }