From b588a7e3e80d1e0dd6e34e448f9d6e7c1d083b1a Mon Sep 17 00:00:00 2001 From: Enrique Date: Thu, 21 Mar 2024 17:28:12 +0100 Subject: [PATCH] [incubator-kie-issues-1016] avoid race condition for awaiting signals and process instance completed (#2403) (#2406) --- .../impl/PerProcessInstanceRuntimeManager.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/jbpm-runtime-manager/src/main/java/org/jbpm/runtime/manager/impl/PerProcessInstanceRuntimeManager.java b/jbpm-runtime-manager/src/main/java/org/jbpm/runtime/manager/impl/PerProcessInstanceRuntimeManager.java index dd46edd7c1..a053f6a49a 100644 --- a/jbpm-runtime-manager/src/main/java/org/jbpm/runtime/manager/impl/PerProcessInstanceRuntimeManager.java +++ b/jbpm-runtime-manager/src/main/java/org/jbpm/runtime/manager/impl/PerProcessInstanceRuntimeManager.java @@ -54,6 +54,7 @@ import org.kie.api.task.TaskService; import org.kie.internal.command.RegistryContext; import org.kie.internal.runtime.manager.Disposable; +import org.kie.internal.runtime.manager.InternalRuntimeEngine; import org.kie.internal.runtime.manager.InternalRuntimeManager; import org.kie.internal.runtime.manager.Mapper; import org.kie.internal.runtime.manager.SessionFactory; @@ -163,11 +164,10 @@ public RuntimeEngine getRuntimeEngine(Context context) { @Override public void signalEvent(String type, Object event) { - // first signal with new context in case there are start event with signal KieSession signalSession = null; Set signalledEngines = new HashSet<>(); - RuntimeEngine runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get()); + RuntimeEngineImpl runtimeEngine = (RuntimeEngineImpl) getRuntimeEngine(ProcessInstanceIdContext.get()); try { // signal execution can rise errors signalSession = runtimeEngine.getKieSession(); @@ -182,18 +182,23 @@ public void signalEvent(String type, Object event) { } // next find out all instances waiting for given event type + runtimeEngine = null; List processInstances = ((InternalMapper) mapper).findContextIdForEvent(type, getIdentifier()); for (String piId : processInstances) { - runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get(Long.parseLong(piId))); try { + runtimeEngine = (RuntimeEngineImpl) getRuntimeEngine(ProcessInstanceIdContext.get(Long.parseLong(piId))); // signal execution can rise errors if (!signalledEngines.contains(runtimeEngine)) { - runtimeEngine.getKieSession().signalEvent(type, event); signalledEngines.add(runtimeEngine); + runtimeEngine.getKieSession().signalEvent(type, event); } + } catch (org.drools.persistence.api.SessionNotFoundException | SessionNotFoundException ex) { + logger.debug("Signal event cannot proceed because of session not found exception {} for process instance id {}", ex.getMessage(), piId); } finally { // ensure we clean up - disposeRuntimeEngine(runtimeEngine); + if (runtimeEngine != null) { + disposeRuntimeEngine(runtimeEngine); + } } }