{"record":{"id":"1d85b8cd4a06f02c","repo":"flowable/flowable-engine","slug":"cannot-work-with-tasks-in-an-active-command","errorCode":null,"errorMessage":"Cannot work with tasks in an active command.","messagePattern":"Cannot work with tasks in an active command\\.","errorType":"exception","errorClass":"FlowableCdiException","httpStatus":null,"severity":"error","filePath":"modules/flowable-cdi/src/main/java/org/flowable/cdi/impl/context/DefaultContextAssociationManager.java","lineNumber":234,"sourceCode":"        } else {\r\n            getScopedAssociation().setVariable(variableName, value);\r\n        }\r\n    }\r\n\r\n    protected ExecutionEntity getExecutionFromContext() {\r\n        if (Context.getCommandContext() != null) {\r\n            ExecutionContext executionContext = ExecutionContextHolder.getExecutionContext();\r\n            if (executionContext != null) {\r\n                return executionContext.getExecution();\r\n            }\r\n        }\r\n        return null;\r\n    }\r\n\r\n    @Override\r\n    public Task getTask() {\r\n        if (Context.getCommandContext() != null) {\r\n            throw new FlowableCdiException(\"Cannot work with tasks in an active command.\");\r\n        }\r\n        return getScopedAssociation().getTask();\r\n    }\r\n\r\n    @Override\r\n    public void setTask(Task task) {\r\n        if (Context.getCommandContext() != null) {\r\n            throw new FlowableCdiException(\"Cannot work with tasks in an active command.\");\r\n        }\r\n        getScopedAssociation().setTask(task);\r\n    }\r\n\r\n    @Override\r\n    public Map<String, Object> getCachedVariables() {\r\n        if (Context.getCommandContext() != null) {\r\n            throw new FlowableCdiException(\"Cannot work with cached variables in an active command.\");\r\n        }\r\n        return getScopedAssociation().getCachedVariables();\r","sourceCodeStart":216,"sourceCodeEnd":252,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-cdi/src/main/java/org/flowable/cdi/impl/context/DefaultContextAssociationManager.java#L216-L252","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Move the getTask() call outside the command: perform it after the engine operation (e.g. taskService.complete) returns, from your own CDI request thread.","Inside delegates/listeners use the DelegateTask passed as a parameter instead of the CDI association manager.","Use TaskService/TaskQuery APIs explicitly with a taskId rather than the ambient CDI-scoped task.","If inside a custom Command, restructure so CDI beans are accessed before commandContextFactory execution or propagate the Task explicitly."],"exampleFix":"// before (inside a JavaDelegate)\nTask t = contextAssociationManager.getTask();\n// after\nTask t = delegateTask; // use the DelegateTask provided by the listener/delegate callback","handlingStrategy":"try-catch","validationCode":"// guard before calling\nif (org.flowable.engine.impl.context.Context.getCommandContext() == null) {\n    task = associationManager.getTask();\n} else {\n    task = delegateTask; // use engine-provided task\n}","typeGuard":"boolean canUseCdiTaskScope() {\n    return org.flowable.engine.impl.context.Context.getCommandContext() == null;\n}","tryCatchPattern":"try {\n    task = associationManager.getTask();\n} catch (org.flowable.cdi.FlowableCdiException e) {\n    task = null; // in-command: resolve via TaskService instead\ntask = taskService.createTaskQuery().executionId(executionId).singleResult();\n}","preventionTips":["Never access CDI task beans from JavaDelegates or listeners; use DelegateTask/DelegateExecution parameters.","Keep CDI-scoped calls in application code, not engine command callbacks.","In async jobs, remember a command context is active — use engine services explicitly.","Wrap CDI business-process interactions in a helper that checks Context.getCommandContext() first."],"tags":["flowable","cdi","command-context","task-scope"],"backgroundTag":"invalid-state-transition","analyzedSha":"d6d39ce1c69ff244f2d9dc6af756a9b95e865586","analyzedAt":"2026-09-11T06:41:19.413Z","contentChangedAt":"2026-09-11T06:41:19.413Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}