flowable/flowable-engine · error · FlowableCdiException

Cannot work with tasks in an active command.

Error message

Cannot work with tasks in an active command.

What it means

DefaultContextAssociationManager.getTask() throws FlowableCdiException when an engine command context is active. CDI-scoped task association state can only be accessed outside engine commands, because during a command the CDI request/ conversation-scoped association is not the authoritative source. This guard prevents reading stale or inconsistent task state from inside a command.

Source

Thrown at modules/flowable-cdi/src/main/java/org/flowable/cdi/impl/context/DefaultContextAssociationManager.java:234

        } else {
            getScopedAssociation().setVariable(variableName, value);
        }
    }

    protected ExecutionEntity getExecutionFromContext() {
        if (Context.getCommandContext() != null) {
            ExecutionContext executionContext = ExecutionContextHolder.getExecutionContext();
            if (executionContext != null) {
                return executionContext.getExecution();
            }
        }
        return null;
    }

    @Override
    public Task getTask() {
        if (Context.getCommandContext() != null) {
            throw new FlowableCdiException("Cannot work with tasks in an active command.");
        }
        return getScopedAssociation().getTask();
    }

    @Override
    public void setTask(Task task) {
        if (Context.getCommandContext() != null) {
            throw new FlowableCdiException("Cannot work with tasks in an active command.");
        }
        getScopedAssociation().setTask(task);
    }

    @Override
    public Map<String, Object> getCachedVariables() {
        if (Context.getCommandContext() != null) {
            throw new FlowableCdiException("Cannot work with cached variables in an active command.");
        }
        return getScopedAssociation().getCachedVariables();

View on GitHub (pinned to d6d39ce1c6)

Solutions

  1. Move the getTask() call outside the command: perform it after the engine operation (e.g. taskService.complete) returns, from your own CDI request thread.
  2. Inside delegates/listeners use the DelegateTask passed as a parameter instead of the CDI association manager.
  3. Use TaskService/TaskQuery APIs explicitly with a taskId rather than the ambient CDI-scoped task.
  4. If inside a custom Command, restructure so CDI beans are accessed before commandContextFactory execution or propagate the Task explicitly.

Example fix

// before (inside a JavaDelegate)
Task t = contextAssociationManager.getTask();
// after
Task t = delegateTask; // use the DelegateTask provided by the listener/delegate callback
Defensive patterns

Strategy: try-catch

Validate before calling

// guard before calling
if (org.flowable.engine.impl.context.Context.getCommandContext() == null) {
    task = associationManager.getTask();
} else {
    task = delegateTask; // use engine-provided task
}

Type guard

boolean canUseCdiTaskScope() {
    return org.flowable.engine.impl.context.Context.getCommandContext() == null;
}

Try / catch

try {
    task = associationManager.getTask();
} catch (org.flowable.cdi.FlowableCdiException e) {
    task = null; // in-command: resolve via TaskService instead
task = taskService.createTaskQuery().executionId(executionId).singleResult();
}

Prevention

When it happens

Trigger: Calling getTask() on the CDI ContextAssociationManager from code that runs inside an engine command, e.g. a JavaDelegate, TaskListener/ExecutionListener body, or any Command<T> executed via managementService/ taskService command execution while Context.getCommandContext() != null.

Common situations: Injecting the CDI-managed task/ business process beans inside a delegate or listener and reading the current task; using CdiBusinessProcess APIs from within a command; calling CDI task beans from asynchronous job execution.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of flowable/flowable-engine@d6d39ce1c6 (2026-09-11). Data as JSON: /api/errors/1d85b8cd4a06f02c. Report an issue: GitHub.