Skip to content

Commit

Permalink
[incubator-kie-issues-1016] avoid race condition for awaiting signals…
Browse files Browse the repository at this point in the history
… and process instance completed (#2403) (#2406)
  • Loading branch information
elguardian authored Mar 21, 2024
1 parent 600f160 commit b588a7e
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<RuntimeEngine> signalledEngines = new HashSet<>();
RuntimeEngine runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get());
RuntimeEngineImpl runtimeEngine = (RuntimeEngineImpl) getRuntimeEngine(ProcessInstanceIdContext.get());
try {
// signal execution can rise errors
signalSession = runtimeEngine.getKieSession();
Expand All @@ -182,18 +182,23 @@ public void signalEvent(String type, Object event) {
}

// next find out all instances waiting for given event type
runtimeEngine = null;
List<String> 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);
}
}
}

Expand Down

0 comments on commit b588a7e

Please sign in to comment.