From 0ac074b176caeb9ec5e62ccece9e6e180bf6f563 Mon Sep 17 00:00:00 2001 From: guoqiang Date: Fri, 30 Jan 2026 11:53:46 +0800 Subject: [PATCH] [fix](Job)Fix NPE in InsertTask when task is canceled When an insert task is canceled during execution, the shared `ctx` object may be set to null. Currently, after `command.runWithUpdateInfo(...)`, the code directly accesses `ctx.getState()`. If `ctx` is null or its state is null, this leads to a NullPointerException (NPE). ### Solution - Cache the `ctx` reference to a local variable to avoid race conditions. - Check both `ctx` and `ctx.getState()` for null before accessing. - If the task was canceled or the state is null, safely return without throwing an exception. - Maintain existing behavior: if the task completes with a non-OK state, still throw `JobException`. ### Impact - Prevents NPE when a task is canceled during execution. - Makes the insert task more robust in concurrent cancel scenarios. --- .../apache/doris/job/extensions/insert/InsertTask.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/job/extensions/insert/InsertTask.java b/fe/fe-core/src/main/java/org/apache/doris/job/extensions/insert/InsertTask.java index b384c35ebdd61b..73eb9ce8efdadf 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/job/extensions/insert/InsertTask.java +++ b/fe/fe-core/src/main/java/org/apache/doris/job/extensions/insert/InsertTask.java @@ -210,8 +210,14 @@ public void run() throws JobException { return; } command.runWithUpdateInfo(ctx, stmtExecutor, loadStatistic); - if (ctx.getState().getStateType() != QueryState.MysqlStateType.OK) { - throw new JobException(ctx.getState().getErrorMessage()); + ConnectContext localCtx = ctx; + if (isCanceled.get()) { + log.info("task has been canceled during execution, task id is {}", getTaskId()); + return; + } + QueryState state = localCtx.getState(); + if (localCtx.getState().getStateType() != QueryState.MysqlStateType.OK) { + throw new JobException(state.getErrorMessage()); } } catch (Exception e) { log.warn("execute insert task error, job id is {}, task id is {},sql is {}", getJobId(),