Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid race condition for awaiting signals and process instance completed #2403

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading