[ 
https://issues.apache.org/jira/browse/ARTEMIS-5562?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Vladimir Polukeev updated ARTEMIS-5562:
---------------------------------------
    Description: 
I have faced with NullPointerException on call closeConnectionFactory method in 
ActiveMQResourceAdapter class at my application.

 

Here is stacktrace:
{noformat}
14:05:18,694 DEBUG [org.apache.activemq.artemis.ra.ActiveMQRAManagedConnection] 
(Thread-1613 (ActiveMQ-client-global-threads)) Cannot invoke 
"org.apache.activemq.artemis.api.core.Pair.getB()" because "pair" is null: 
java.lang.NullPointerException: Cannot invoke 
"org.apache.activemq.artemis.api.core.Pair.getB()" because "pair" is null
                at 
deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQResourceAdapter.closeConnectionFactory(ActiveMQResourceAdapter.java:1924)
                at 
deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRAManagedConnection.destroy(ActiveMQRAManagedConnection.java:253)
                at 
org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.listener.AbstractConnectionListener.destroy(AbstractConnectionListener.java:637)
                at 
org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreConcurrentLinkedDequeManagedConnectionPool.getConnection(SemaphoreConcurrentLinkedDequeManagedConnectionPool.java:459)
                at 
org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.pool.AbstractPool.getTransactionNewConnection(AbstractPool.java:722)
                at 
org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.pool.AbstractPool.getConnection(AbstractPool.java:618)
                at 
org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.AbstractConnectionManager.getManagedConnection(AbstractConnectionManager.java:624)
                at 
org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:440)
                at 
org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.java:789)
                at 
deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRASessionFactoryImpl.allocateConnection(ActiveMQRASessionFactoryImpl.java:792)
                at 
deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRASessionFactoryImpl.createSession(ActiveMQRASessionFactoryImpl.java:482)
                at 
deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRASessionFactoryImpl.createSession(ActiveMQRASessionFactoryImpl.java:673)
                at 
deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRASessionFactoryImpl.createSession(ActiveMQRASessionFactoryImpl.java:678)
                at 
deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl.validateUser(ActiveMQRAConnectionFactoryImpl.java:422)
                at 
deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl.createContext(ActiveMQRAConnectionFactoryImpl.java:379)
                at 
deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl.createContext(ActiveMQRAConnectionFactoryImpl.java:394)
                at 
org.wildfly.extension.messaging-activemq.injection@28.0.1.Final//org.wildfly.extension.messaging.activemq.deployment.injection.AbstractJMSContext.createContext(AbstractJMSContext.java:57)
                at 
org.wildfly.extension.messaging-activemq.injection@28.0.1.Final//org.wildfly.extension.messaging.activemq.deployment.injection.AbstractJMSContext.lambda$getContext$0(AbstractJMSContext.java:47)
                at 
java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708)
                at 
org.wildfly.extension.messaging-activemq.injection@28.0.1.Final//org.wildfly.extension.messaging.activemq.deployment.injection.AbstractJMSContext.getContext(AbstractJMSContext.java:46)
 {noformat}
Here is source code from Artemis Resource Adapter:
{code:java}
   public synchronized void closeConnectionFactory(ConnectionFactoryProperties 
properties) {
      Pair<ActiveMQConnectionFactory, AtomicInteger> pair = 
knownConnectionFactories.get(properties);
      int references = pair.getB().decrementAndGet();
      if (pair.getA() != null && pair.getA() != 
defaultActiveMQConnectionFactory && references == 0) {
         knownConnectionFactories.remove(properties).getA().close();
      }
   } {code}
Exception has been thrown on "int references = pair.getB().decrementAndGet();" 
line.

I artificially reproduce NPE by this test:
{noformat}
public class SyncTest {
    private static final TestClass TEST_CLASS = new TestClass();    @Test
    
    public void test() throws InterruptedException {
        Runnable task = TEST_CLASS::test;
        var thread1 = new Thread(task);
        var thread2 = new Thread(task);        

        thread1.start();
        thread2.start();        

        thread1.join();
        thread2.join();
    }    


    private static class TestClass {
        private final Map<String, AtomicInteger> map = new HashMap<>();        
        {
            map.put("test", new AtomicInteger(1));
        }        
        

        private synchronized void test() {
            System.out.println("Current thread : " + 
Thread.currentThread().getName());
            var value = map.get("test");
            value.decrementAndGet();
            if (value.get() == 0) {
                map.remove("test");
            }
        }
    }
}{noformat}
Test shows case when two separated threads invoke the same method and the last 
thread throws NPE due to there is no object in HashMap because it has been 
removed by previous thread. 

 

As I understand the error case, two separated threads invoke 
closeConnectionFactory method. And the first thread remove pair from HashMap. 
And the second thread throws NPE. It looks like there is an error around 
AtomicInteger counter (object B in pair) logic. But it is only my suggestion.

 

  was:
I have faced with NullPointerException on call closeConnectionFactory method in 
ActiveMQResourceAdapter class at my application.

 

Here is stacktrace:
{noformat}
14:05:18,694 DEBUG [org.apache.activemq.artemis.ra.ActiveMQRAManagedConnection] 
(Thread-1613 (ActiveMQ-client-global-threads)) Cannot invoke 
"org.apache.activemq.artemis.api.core.Pair.getB()" because "pair" is null: 
java.lang.NullPointerException: Cannot invoke 
"org.apache.activemq.artemis.api.core.Pair.getB()" because "pair" is null
                at 
deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQResourceAdapter.closeConnectionFactory(ActiveMQResourceAdapter.java:1924)
                at 
deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRAManagedConnection.destroy(ActiveMQRAManagedConnection.java:253)
                at 
org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.listener.AbstractConnectionListener.destroy(AbstractConnectionListener.java:637)
                at 
org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreConcurrentLinkedDequeManagedConnectionPool.getConnection(SemaphoreConcurrentLinkedDequeManagedConnectionPool.java:459)
                at 
org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.pool.AbstractPool.getTransactionNewConnection(AbstractPool.java:722)
                at 
org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.pool.AbstractPool.getConnection(AbstractPool.java:618)
                at 
org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.AbstractConnectionManager.getManagedConnection(AbstractConnectionManager.java:624)
                at 
org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:440)
                at 
org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.java:789)
                at 
deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRASessionFactoryImpl.allocateConnection(ActiveMQRASessionFactoryImpl.java:792)
                at 
deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRASessionFactoryImpl.createSession(ActiveMQRASessionFactoryImpl.java:482)
                at 
deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRASessionFactoryImpl.createSession(ActiveMQRASessionFactoryImpl.java:673)
                at 
deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRASessionFactoryImpl.createSession(ActiveMQRASessionFactoryImpl.java:678)
                at 
deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl.validateUser(ActiveMQRAConnectionFactoryImpl.java:422)
                at 
deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl.createContext(ActiveMQRAConnectionFactoryImpl.java:379)
                at 
deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl.createContext(ActiveMQRAConnectionFactoryImpl.java:394)
                at 
org.wildfly.extension.messaging-activemq.injection@28.0.1.Final//org.wildfly.extension.messaging.activemq.deployment.injection.AbstractJMSContext.createContext(AbstractJMSContext.java:57)
                at 
org.wildfly.extension.messaging-activemq.injection@28.0.1.Final//org.wildfly.extension.messaging.activemq.deployment.injection.AbstractJMSContext.lambda$getContext$0(AbstractJMSContext.java:47)
                at 
java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708)
                at 
org.wildfly.extension.messaging-activemq.injection@28.0.1.Final//org.wildfly.extension.messaging.activemq.deployment.injection.AbstractJMSContext.getContext(AbstractJMSContext.java:46)
 {noformat}
Here is source code from Artemis Resource Adapter:
{code:java}
   public synchronized void closeConnectionFactory(ConnectionFactoryProperties 
properties) {
      Pair<ActiveMQConnectionFactory, AtomicInteger> pair = 
knownConnectionFactories.get(properties);
      int references = pair.getB().decrementAndGet();
      if (pair.getA() != null && pair.getA() != 
defaultActiveMQConnectionFactory && references == 0) {
         knownConnectionFactories.remove(properties).getA().close();
      }
   } {code}
Exception has been thrown on int references = pair.getB().decrementAndGet(); 
line.

I artificially reproduce NPE by this test:
{noformat}
public class SyncTest {
    private static final TestClass TEST_CLASS = new TestClass();    @Test
    
    public void test() throws InterruptedException {
        Runnable task = TEST_CLASS::test;
        var thread1 = new Thread(task);
        var thread2 = new Thread(task);        

        thread1.start();
        thread2.start();        

        thread1.join();
        thread2.join();
    }    


    private static class TestClass {
        private final Map<String, AtomicInteger> map = new HashMap<>();        
        {
            map.put("test", new AtomicInteger(1));
        }        
        

        private synchronized void test() {
            System.out.println("Current thread : " + 
Thread.currentThread().getName());
            var value = map.get("test");
            value.decrementAndGet();
            if (value.get() == 0) {
                map.remove("test");
            }
        }
    }
}{noformat}
Test shows case when two separated threads invoke the same method and the last 
thread throws NPE due to there is no object in HashMap because it has been 
removed by previous thread. 

 

As I understand the error case, two separated threads invoke 
closeConnectionFactory method. And the first thread remove pair from HashMap. 
And the second thread throws NPE. It looks like there is an error around 
AtomicInteger counter (object B in pair) logic. But it is only my suggestion.

 


> NullPointerException on call closeConnectionFactory method in 
> ActiveMQResourceAdapter class 
> --------------------------------------------------------------------------------------------
>
>                 Key: ARTEMIS-5562
>                 URL: https://issues.apache.org/jira/browse/ARTEMIS-5562
>             Project: ActiveMQ Artemis
>          Issue Type: Bug
>    Affects Versions: 2.30.0
>            Reporter: Vladimir Polukeev
>            Priority: Major
>
> I have faced with NullPointerException on call closeConnectionFactory method 
> in ActiveMQResourceAdapter class at my application.
>  
> Here is stacktrace:
> {noformat}
> 14:05:18,694 DEBUG 
> [org.apache.activemq.artemis.ra.ActiveMQRAManagedConnection] (Thread-1613 
> (ActiveMQ-client-global-threads)) Cannot invoke 
> "org.apache.activemq.artemis.api.core.Pair.getB()" because "pair" is null: 
> java.lang.NullPointerException: Cannot invoke 
> "org.apache.activemq.artemis.api.core.Pair.getB()" because "pair" is null
>                 at 
> deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQResourceAdapter.closeConnectionFactory(ActiveMQResourceAdapter.java:1924)
>                 at 
> deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRAManagedConnection.destroy(ActiveMQRAManagedConnection.java:253)
>                 at 
> org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.listener.AbstractConnectionListener.destroy(AbstractConnectionListener.java:637)
>                 at 
> org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreConcurrentLinkedDequeManagedConnectionPool.getConnection(SemaphoreConcurrentLinkedDequeManagedConnectionPool.java:459)
>                 at 
> org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.pool.AbstractPool.getTransactionNewConnection(AbstractPool.java:722)
>                 at 
> org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.pool.AbstractPool.getConnection(AbstractPool.java:618)
>                 at 
> org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.AbstractConnectionManager.getManagedConnection(AbstractConnectionManager.java:624)
>                 at 
> org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:440)
>                 at 
> org.jboss.ironjacamar.impl@3.0.2.Final//org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.java:789)
>                 at 
> deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRASessionFactoryImpl.allocateConnection(ActiveMQRASessionFactoryImpl.java:792)
>                 at 
> deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRASessionFactoryImpl.createSession(ActiveMQRASessionFactoryImpl.java:482)
>                 at 
> deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRASessionFactoryImpl.createSession(ActiveMQRASessionFactoryImpl.java:673)
>                 at 
> deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRASessionFactoryImpl.createSession(ActiveMQRASessionFactoryImpl.java:678)
>                 at 
> deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl.validateUser(ActiveMQRAConnectionFactoryImpl.java:422)
>                 at 
> deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl.createContext(ActiveMQRAConnectionFactoryImpl.java:379)
>                 at 
> deployment.artemis-jakarta-rar-2.30.0.rar//org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl.createContext(ActiveMQRAConnectionFactoryImpl.java:394)
>                 at 
> org.wildfly.extension.messaging-activemq.injection@28.0.1.Final//org.wildfly.extension.messaging.activemq.deployment.injection.AbstractJMSContext.createContext(AbstractJMSContext.java:57)
>                 at 
> org.wildfly.extension.messaging-activemq.injection@28.0.1.Final//org.wildfly.extension.messaging.activemq.deployment.injection.AbstractJMSContext.lambda$getContext$0(AbstractJMSContext.java:47)
>                 at 
> java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708)
>                 at 
> org.wildfly.extension.messaging-activemq.injection@28.0.1.Final//org.wildfly.extension.messaging.activemq.deployment.injection.AbstractJMSContext.getContext(AbstractJMSContext.java:46)
>  {noformat}
> Here is source code from Artemis Resource Adapter:
> {code:java}
>    public synchronized void 
> closeConnectionFactory(ConnectionFactoryProperties properties) {
>       Pair<ActiveMQConnectionFactory, AtomicInteger> pair = 
> knownConnectionFactories.get(properties);
>       int references = pair.getB().decrementAndGet();
>       if (pair.getA() != null && pair.getA() != 
> defaultActiveMQConnectionFactory && references == 0) {
>          knownConnectionFactories.remove(properties).getA().close();
>       }
>    } {code}
> Exception has been thrown on "int references = 
> pair.getB().decrementAndGet();" line.
> I artificially reproduce NPE by this test:
> {noformat}
> public class SyncTest {
>     private static final TestClass TEST_CLASS = new TestClass();    @Test
>     
>     public void test() throws InterruptedException {
>         Runnable task = TEST_CLASS::test;
>         var thread1 = new Thread(task);
>         var thread2 = new Thread(task);        
>         thread1.start();
>         thread2.start();        
>         thread1.join();
>         thread2.join();
>     }    
>     private static class TestClass {
>         private final Map<String, AtomicInteger> map = new HashMap<>();       
>  
>         {
>             map.put("test", new AtomicInteger(1));
>         }        
>         
>         private synchronized void test() {
>             System.out.println("Current thread : " + 
> Thread.currentThread().getName());
>             var value = map.get("test");
>             value.decrementAndGet();
>             if (value.get() == 0) {
>                 map.remove("test");
>             }
>         }
>     }
> }{noformat}
> Test shows case when two separated threads invoke the same method and the 
> last thread throws NPE due to there is no object in HashMap because it has 
> been removed by previous thread. 
>  
> As I understand the error case, two separated threads invoke 
> closeConnectionFactory method. And the first thread remove pair from HashMap. 
> And the second thread throws NPE. It looks like there is an error around 
> AtomicInteger counter (object B in pair) logic. But it is only my suggestion.
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@activemq.apache.org
For additional commands, e-mail: issues-h...@activemq.apache.org
For further information, visit: https://activemq.apache.org/contact


Reply via email to