Bug 1326329

Summary: Race condition iterating the ksessions created from a kbase
Product: [Retired] JBoss BRMS Platform 6 Reporter: Mario Fusco <mfusco>
Component: BREAssignee: Mario Fusco <mfusco>
Status: CLOSED EOL QA Contact: Marek Winkler <mwinkler>
Severity: urgent Docs Contact:
Priority: urgent    
Version: 6.3.0CC: etirelli, lpetrovi, mfusco, rrajasek
Target Milestone: CR1Keywords: Regression
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2020-03-27 19:03:25 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:

Description Mario Fusco 2016-04-12 12:31:37 UTC
The implementation of the improvement described here https://issues.jboss.org/browse/DROOLS-1110 caused a race condition because a ksession could be disposed and removed from the kbase's sessions list while another thread is iterating. When this happens the following exception is thrown:


Exception in thread "Thread-28" java.util.ConcurrentModificationException
        at java.util.HashMap$HashIterator.nextNode(HashMap.java:1429)
        at java.util.HashMap$KeyIterator.next(HashMap.java:1453)
        at org.drools.core.impl.KnowledgeBaseImpl.tryDeactivateAllSessions(KnowledgeBaseImpl.java:756)
        at org.drools.core.impl.KnowledgeBaseImpl.tryLockAndDeactivate(KnowledgeBaseImpl.java:777)
        at org.drools.core.impl.KnowledgeBaseImpl.enqueueModification(KnowledgeBaseImpl.java:714)
        at org.drools.compiler.kie.builder.impl.KieContainerImpl.update(KieContainerImpl.java:184)
        at org.drools.compiler.kie.builder.impl.KieContainerImpl.updateToVersion(KieContainerImpl.java:131)
        at org.drools.compiler.integrationtests.KieContainerTest$1.run(KieContainerTest.java:202)
        at java.lang.Thread.run(Thread.java:745)

Comment 3 Mario Fusco 2016-04-12 12:49:12 UTC
Cherry-picked to 6.4.x by https://github.com/droolsjbpm/drools/commit/1d91b8dd5

Comment 4 Marek Winkler 2016-04-27 06:40:53 UTC
It seems that the ConcurrentModificationException can still appear if you iterate the list of KieSessions obtained from KieBase.getKieSessions() and dispose them, for instance:

  for (KieSession kieSession : kieBase.getKieSessions()) {
      kieSession.dispose();
  }

Please see PR with test: https://github.com/droolsjbpm/drools/pull/761

This used to work in BxMS 6.3.0 ER1 (probably before DROOLS-1110).

There is also a workaround by copying the KieSessions collection before iterating it, such as:

  Collection<KieSession> kieSessions = new ArrayList<KieSession>();
  kieSessions.addAll(this.kieBase.getKieSessions());
  for (KieSession kieSession : kieSessions) {
      kieSession.dispose();
  }

Is this change in behavior expected for 6.3 product? (If so, we must document it.)

Comment 5 Mario Fusco 2016-04-27 07:16:37 UTC
Fixed on master by https://github.com/droolsjbpm/drools/compare/8eadcaa...a62d5e9

Comment 6 Mario Fusco 2016-04-27 07:19:48 UTC
Cherry-picked to 6.4.x branch with https://github.com/droolsjbpm/drools/compare/58ddfd1...a1f989f