{"record":{"id":"9968f21665075c1f","repo":"flowable/flowable-engine","slug":"cannot-work-with-cached-variables-in-an-active-com","errorCode":null,"errorMessage":"Cannot work with cached variables in an active command.","messagePattern":"Cannot work with cached variables 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":250,"sourceCode":"    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\n    }\r\n\r\n}\r\n","sourceCodeStart":232,"sourceCodeEnd":256,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-cdi/src/main/java/org/flowable/cdi/impl/context/DefaultContextAssociationManager.java#L232-L256","documentation":"DefaultContextAssociationManager.getCachedVariables() throws FlowableCdiException when an engine command context is active. Cached CDI-scoped variables are only valid outside commands; reading them during a command risks returning variables that do not match the engine's current variable state.","triggerScenarios":"Invoking getCachedVariables() on the CDI ContextAssociationManager from within an active command — delegates, listeners, or code executed via CommandContext (Context.getCommandContext() != null).","commonSituations":"Reading business-process cached variables inside a JavaDelegate; accessing CdiBusinessProcess variables from a listener or from a job/async executor thread where a command is active.","solutions":["Read variables from the DelegateExecution/DelegateTask passed into the callback (execution.getVariable / task.getVariables).","Move getCachedVariables() calls outside the command, after the engine operation returns.","Use RuntimeService/TaskService variable APIs with explicit executionId/taskId.","Refactor custom Commands to collect variables before entering or after leaving the command."],"exampleFix":"// before (inside a JavaDelegate)\nMap<String,Object> vars = associationManager.getCachedVariables();\n// after\nMap<String,Object> vars = delegateExecution.getVariables();","handlingStrategy":"type-guard","validationCode":"Map<String,Object> safeGetVars(DelegateExecution ex) {\n    return org.flowable.engine.impl.context.Context.getCommandContext() != null\n        ? ex.getVariables()\n        : associationManager.getCachedVariables();\n}","typeGuard":"boolean canReadCachedVariables() {\n    return org.flowable.engine.impl.context.Context.getCommandContext() == null;\n}","tryCatchPattern":"try {\n    return associationManager.getCachedVariables();\n} catch (org.flowable.cdi.FlowableCdiException e) {\n    return runtimeService.getVariables(executionId); // fallback to engine service\n}","preventionTips":["Inside callbacks read variables from DelegateExecution/DelegateTask, never the CDI cache.","Use RuntimeService/TaskService variable APIs with explicit ids when a command is active.","Keep CDI variable caching usage in request-scoped application code only."],"tags":["flowable","cdi","command-context","variables"],"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-14T11:17:12.474Z"}