Skip to content

NullPointerException instead of OptimisticLockingException when the process instance is deleted while a job loads the execution tree #4017

Open

Description

@shardtVLH

Describe the bug

When a job (e.g. a timer intermediate catch event inside an embedded subprocess) is executed while
another transaction deletes or terminates the process instance, the engine fails with a
NullPointerException in ExecutionEntity.ensureExecutionTreeInitialized instead of an
OptimisticLockingException.

JobEntity.execute loads only the job's execution. When the handler later calls
execution.getProcessInstance() (e.g. from PvmExecutionImpl.getDelayedEvents()), the execution
tree is loaded with findExecutionsByProcessInstanceId. If the process instance was deleted and
committed in between, the list is empty, procInstance stays null and
procInstance.restoreProcessInstance(...) throws.

Because the exception is not an OptimisticLockingException, JobRetryCmd.shouldDecrementRetriesFor
decrements the retries and an incident is created after the retries are exhausted, although there
is nothing left to do: the process instance is gone. The same race on an INSERT is already mapped
to an OptimisticLockingException via enableOptimisticLockingOnForeignKeyViolation.

To Reproduce

  1. Deploy a process with a timer intermediate catch event inside an embedded subprocess (so the
    timer's execution is not the process instance execution).
  2. Start an instance and run a command that loads the timer's execution but not yet the execution
    tree (e.g. a ControllableCommand that calls findExecutionById, syncs, then
    execution.getProcessInstance()).
  3. Delete the process instance from another transaction.
  4. Continue the first command.

Observed: NullPointerException: Cannot invoke "ExecutionEntity.restoreProcessInstance(...)" because "procInstance" is null (logged as ENGINE-16004 when the job handler fails, or ENGINE-20009
when a call activity completes).

Expected behavior

An OptimisticLockingException, so that the job executor retries without decrementing the retries
and then skips the job that no longer exists.

Additional context

  • Observed with Operaton 2.1.1; the code is unchanged on main.
  • Database-independent: it depends only on when the concurrent deletion becomes visible
    (READ COMMITTED). Reproducible with H2 in the engine test suite.

Stack trace (engine frames, Operaton 2.1.1):

ENGINE-16004 Exception while closing command context: Cannot invoke
"org.operaton.bpm.engine.impl.persistence.entity.ExecutionEntity.restoreProcessInstance(...)"
because "procInstance" is null
java.lang.NullPointerException: ...
	at org.operaton.bpm.engine.impl.persistence.entity.ExecutionEntity.ensureExecutionTreeInitialized(ExecutionEntity.java:1313)
	at org.operaton.bpm.engine.impl.persistence.entity.ExecutionEntity.ensureProcessInstanceInitialized(ExecutionEntity.java:805)
	at org.operaton.bpm.engine.impl.persistence.entity.ExecutionEntity.getProcessInstance(ExecutionEntity.java:794)
	at org.operaton.bpm.engine.impl.persistence.entity.ExecutionEntity.getProcessInstance(ExecutionEntity.java:97)
	at org.operaton.bpm.engine.impl.pvm.runtime.PvmExecutionImpl.getDelayedEvents(PvmExecutionImpl.java:1984)
	at org.operaton.bpm.engine.impl.pvm.runtime.PvmExecutionImpl.dispatchDelayedEventsAndPerformOperation(PvmExecutionImpl.java:2021)
	at org.operaton.bpm.engine.impl.pvm.runtime.PvmExecutionImpl.dispatchDelayedEventsAndPerformOperation(PvmExecutionImpl.java:2006)
	at org.operaton.bpm.engine.impl.bpmn.behavior.FlowNodeActivityBehavior.leave(FlowNodeActivityBehavior.java:55)
	at org.operaton.bpm.engine.impl.bpmn.behavior.IntermediateCatchEventActivityBehavior.signal(IntermediateCatchEventActivityBehavior.java:51)
	at org.operaton.bpm.engine.impl.pvm.runtime.PvmExecutionImpl.signal(PvmExecutionImpl.java:762)
	at org.operaton.bpm.engine.impl.jobexecutor.TimerCatchIntermediateEventJobHandler.execute(TimerCatchIntermediateEventJobHandler.java:46)
	at org.operaton.bpm.engine.impl.jobexecutor.TimerCatchIntermediateEventJobHandler.execute(TimerCatchIntermediateEventJobHandler.java:27)
	at org.operaton.bpm.engine.impl.persistence.entity.JobEntity.execute(JobEntity.java:123)
	at org.operaton.bpm.engine.impl.cmd.ExecuteJobsCmd.execute(ExecuteJobsCmd.java:106)
	at org.operaton.bpm.engine.impl.cmd.ExecuteJobsCmd.execute(ExecuteJobsCmd.java:41)
	at org.operaton.bpm.engine.impl.interceptor.CommandExecutorImpl.execute(CommandExecutorImpl.java:29)
	at org.operaton.bpm.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:113)
	...
	at org.operaton.bpm.engine.impl.jobexecutor.ExecuteJobHelper.executeJob(ExecuteJobHelper.java:62)
	at org.operaton.bpm.engine.impl.ManagementServiceImpl.executeJob(ManagementServiceImpl.java:124)

Proposed fix

Throw an OptimisticLockingException in ensureExecutionTreeInitialized when no process instance
execution is found. I'm working on this and will open a pull request with a concurrency test.

Activity

  1. shardtVLH commented on Oct 30, 2026

    @shardtVLH
    Author

    I'm working on this and will open a pull request with a fix and a concurrency test shortly.

  2. github-actions commented on Oct 30, 2026

    @github-actions
    Contributor

    Hey @shardtVLH! 🎉

    Thanks for opening your first issue. We really appreciate your interest and contribution. 🙌

    👉 Before moving forward, please make sure to check out our Contributing Guidelines and the Code of Conduct. This helps us keep everything clear and consistent.

    If you enjoy this project, consider giving it a star ⭐️ — we’d really appreciate that.

    Looking forward to your input! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions