apache/dolphinscheduler · error · TaskException

Failed to cancel EMR Serverless job run

Error message

Failed to cancel EMR Serverless job run

What it means

cancelApplication calls CancelJobRun to stop a running EMR Serverless job run. Any AWS SDK SdkBaseException during that call is rethrown as TaskException with this message. Note EmrServerlessTaskException is not caught here, so API validation errors surface directly.

Source

Thrown at dolphinscheduler-task-plugin/dolphinscheduler-task-emr-serverless/src/main/java/org/apache/dolphinscheduler/plugin/task/emrserverless/EmrServerlessTask.java:195

        }
    }

    @Override
    public void cancelApplication() throws TaskException {
        if (StringUtils.isEmpty(jobRunId)) {
            log.warn("jobRunId is empty, skip cancel");
            return;
        }
        log.info("Cancelling EMR Serverless job run, applicationId: {}, jobRunId: {}",
                emrServerlessParameters.getApplicationId(), jobRunId);
        try {
            CancelJobRunRequest request = new CancelJobRunRequest()
                    .withApplicationId(emrServerlessParameters.getApplicationId())
                    .withJobRunId(jobRunId);
            CancelJobRunResult result = emrServerlessClient.cancelJobRun(request);
            log.info("Cancel job run result: {}", result);
        } catch (SdkBaseException e) {
            throw new TaskException("Failed to cancel EMR Serverless job run", e);
        }
    }

    @Override
    public List<String> getApplicationIds() throws TaskException {
        return Collections.emptyList();
    }

    @Override
    public AbstractParameters getParameters() {
        return emrServerlessParameters;
    }

    /**
     * Build StartJobRunRequest from parameters and user-provided JSON.
     */
    private StartJobRunRequest buildStartJobRunRequest() {
        String startJobRunRequestJson;

View on GitHub (pinned to 02eac45a1b)

Solutions

  1. Check the 'caused by' AWS error: if the job run is already terminal, the cancel is unnecessary — treat as benign
  2. Verify IAM permissions include emr-serverless:CancelJobRun
  3. Confirm applicationId/jobRunId and region match the actual job run
  4. If frequent, wrap the cancel so already-terminal-state errors are logged as warnings instead of failing the kill flow

Example fix

// before
catch (SdkBaseException e) {
    throw new TaskException("Failed to cancel EMR Serverless job run", e);
}
// after
catch (SdkBaseException e) {
    if (String.valueOf(e).contains("InvalidRequestException")) {
        log.warn("Job run already in terminal state, skip cancel", e);
    } else {
        throw new TaskException("Failed to cancel EMR Serverless job run", e);
    }
}
Defensive patterns

Strategy: try-catch

Validate before calling

// check job run state before cancel; terminal states reject CancelJobRun
String state = emrServerlessClient.getJobRun(new GetJobRunRequest()
    .withApplicationId(appId).withJobRunId(jobRunId)).getJobRun().getState();
boolean cancelable = state.equals("SUBMITTED") || state.equals("PENDING")
    || state.equals("SCHEDULED") || state.equals("RUNNING");

Type guard

boolean isTerminalJobRunState(String s) {
    return s == null || java.util.Set.of("SUCCESS","FAILED","CANCELLED","CANCEL_PENDING").contains(s);
}

Try / catch

try {
    emrServerlessClient.cancelJobRun(request);
} catch (SdkBaseException e) {
    if (e instanceof AmazonServiceException
            && ((AmazonServiceException) e).getErrorCode().contains("InvalidRequest")) {
        log.warn("Job run already terminal, cancel not needed");
    } else {
        throw new TaskException("Failed to cancel EMR Serverless job run", e);
    }
}

Prevention

When it happens

Trigger: emrServerlessClient.cancelJobRun(request) throws: job run already in a terminal state (ValidationException/ConflictException), wrong applicationId/jobRunId, network failure, or credentials lacking emr-serverless:CancelJobRun permission.

Common situations: Kill arrives after the job already finished (state SUCCESS/FAILED/CANCELLED -> AWS rejects cancel); IAM role missing CancelJobRun; stale jobRunId from a recovered failover; region mismatch between client config and the application.

Understand the failure class

Background: "API request failed": what wrapped HTTP errors from external APIs mean and how to find the real cause — this error's family across 29 libraries.

Related errors


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