Bug 1326329 - Race condition iterating the ksessions created from a kbase
Summary: Race condition iterating the ksessions created from a kbase
Keywords:
Status: CLOSED EOL
Alias: None
Product: JBoss BRMS Platform 6
Classification: Retired
Component: BRE
Version: 6.3.0
Hardware: Unspecified
OS: Unspecified
urgent
urgent
Target Milestone: CR1
: ---
Assignee: Mario Fusco
QA Contact: Marek Winkler
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2016-04-12 12:31 UTC by Mario Fusco
Modified: 2020-03-27 19:03 UTC (History)
4 users (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2020-03-27 19:03:25 UTC
Type: Bug


Attachments (Terms of Use)


Links
System ID Private Priority Status Summary Last Updated
Red Hat Issue Tracker DROOLS-1110 0 Major Closed Avoid acquiring a kbase readlock when performing a working memory action 2016-11-08 09:47:16 UTC

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


Note You need to log in before you can comment on or make changes to this bug.