apache/dolphinscheduler · error · TaskException

Cancel sql task failed

Error message

Cancel sql task failed

What it means

SqlTask.cancel() attempts to cancel the running JDBC statement and sets the exit code to KILL. If any exception occurs while cancelling the session statement (or in the cancel block), the exception is wrapped and rethrown as a TaskException with this message.

Source

Thrown at dolphinscheduler-task-plugin/dolphinscheduler-task-sql/src/main/java/org/apache/dolphinscheduler/plugin/task/sql/SqlTask.java:186

            if (exitStatusCode == TaskConstants.EXIT_CODE_KILL) {
                log.info("sql task has been killed");
                return;
            }
            setExitStatusCode(TaskConstants.EXIT_CODE_FAILURE);
            log.error("sql task error", e);
            throw new TaskException("Execute sql task failed", e);
        }
    }

    @Override
    public void cancel() throws TaskException {
        try {
            if (sessionStatement != null) {
                sessionStatement.cancel();
            }
            exitStatusCode = TaskConstants.EXIT_CODE_KILL;
        } catch (Exception e) {
            throw new TaskException("Cancel sql task failed", e);
        }
    }

    /**
     * execute function and sql
     *
     * @param mainStatementsBinds main statements binds
     * @param preStatementsBinds  pre statements binds
     * @param postStatementsBinds post statements binds
     */
    public void executeFuncAndSql(List<SqlBinds> mainStatementsBinds,
                                  List<SqlBinds> preStatementsBinds,
                                  List<SqlBinds> postStatementsBinds) throws Exception {
        try (
                Connection connection =
                        DataSourceClientProvider.getAdHocConnection(DbType.valueOf(sqlParameters.getType()),
                                baseConnectionParam)) {
            sessionConnection = connection;

View on GitHub (pinned to 02eac45a1b)

Solutions

  1. Check database connectivity and driver support for Statement.cancel(); upgrade the JDBC driver if cancel is unsupported
  2. Inspect the wrapped cause exception in the task log to identify the real failure (closed connection, timeout, etc.)
  3. Retry the kill; if the statement already completed, the task result is usually still valid
  4. Ensure the task's datasource connection pool is not forcibly closing connections before cancel is invoked

Example fix

// before
try {
    sessionStatement.cancel();
} catch (Exception e) {
    throw new TaskException("Cancel sql task failed", e);
}
// after
try {
    if (sessionStatement != null && !sessionStatement.isClosed()) {
        sessionStatement.cancel();
    }
} catch (Exception e) {
    log.warn("Cancel sql task failed, statement may already be closed", e);
    exitStatusCode = TaskConstants.EXIT_CODE_KILL;
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (sessionStatement != null) {
    try {
        sessionStatement.isClosed(); // probe connection health before cancel
    } catch (Exception e) {
        log.warn("Statement/connection already broken, skip cancel");
    }
}

Type guard

boolean cancellable(java.sql.Statement st) {
    try { return st != null && !st.isClosed(); } catch (Exception e) { return false; }
}

Try / catch

try {
    sqlTask.cancel();
} catch (TaskException e) {
    // inspect e.getCause(): SQLRecoverableException -> connection lost; SQLFeatureNotSupported -> driver can't cancel
    log.error("Cancel failed, cause: {}", e.getCause(), e);
}

Prevention

When it happens

Trigger: Calling cancel on a SqlTask whose underlying Statement.cancel() throws (e.g. the connection is already closed, the driver does not support cancellation, or a network drop to the database occurred during the cancel request).

Common situations: Killing a long-running SQL task from the DS UI; worker shutdown/fault tolerance interrupting tasks; database failover mid-cancel; drivers (e.g. some Hive/JDBC versions) that throw on cancel of finished statements.

Understand the failure class

Background: "query failed", "%w: SQL error" — wrapped database query errors in Go libraries explained — this error's family across 3 libraries.

Related errors


AI-assisted analysis of apache/dolphinscheduler@02eac45a1b (2026-09-06). Data as JSON: /api/errors/a4a4dc51baec8998. Report an issue: GitHub.