<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="https://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    Running inot a deadlock because of messageListener handling
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="https://community.jboss.org/people/nomind">sameer saurabh</a> in <i>JBoss Messaging</i> - <a href="https://community.jboss.org/message/773764#773764">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>The scenario for deadlock in JMS MessageListener while closing resources in onException() method of ExceptionListener:</p><p> &bull;&#160;&#160;&#160;&#160;&#160;When an exception occurs in a MessageListener thread, it calls the asynchFailure() method of Session class which in turns calls the asynchFailure() method of Connection class. In the Connection class, a lock is taken on &#8216;elLock&rsquo; object and if there is no ExceptionListenerRunnable thread running, a ExceptionListenerRunnable thread is created and started. In the run() method of ExceptionListenerRunnable, a lock is again taken on &#8216;elLock&rsquo; object and the onException() method of ExceptionListener class is invoked.</p><p> &bull;&#160;&#160;&#160;&#160;&#160;In onException() method of ExceptionListener class we try to recreate the JMS Connection. Before reconnecting we close all existing resources (Consumer, Session and Connection). In the close() API of SpyMessageConsumer class, the Message Consumer checks for any MessageListener thread and if there is any listener thread it calls the join() API of Thread class and wait for the listener thread to die.</p><p> &bull;&#160;&#160;&#160;&#160;&#160;In the meanwhile if one more exception occurs in MessageListener thread, it again invokes asynchFailure() method of Connection class. If the earlier ExceptionListenerRunnable thread has not released the lock on the &#8216;elLock&rsquo; object, the MessageListener thread waits outside the synchronized block in asynchFailure() method of Connection class.</p><p> This leads to a deadlock situation where ExceptionListener thread is waiting for the MessageListener thread to die and MessageListener thread is waiting for the ExceptionListener class to release lock on &#8216;elLock&rsquo; object.</p><p> Though in asynchFailure() method of Connection class, a check is there to verify and log a warning message (&ldquo;Connection failure, already in the exception listener&#8221;) if the ExceptionListenerRunnable thread is already running but this check is also inside the synchronized block. So even if ExceptionListenerRunnable thread is already running, the other thread will not be able to take the lock on &#8216;elLock&rsquo; object and the warning message will never be displayed. This seems to be a bug in JMS code as the check to verify an already running ExceptionListenerRunnable thread should be outside the synchronized block and if the ExceptionListenerRunnable thread is already running, the method should return from there.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Following is my thread dump - </p><p>"ExceptionListener Connection@206865307[token=ConnectionToken:ID:44/ee177c7bd4386f174c32bc563cb9b67a rcvstate=STARTED]"Id=331 in WAITING on lock=java.lang.Thread@13f1442e</p><p>&#160;&#160;&#160; at java.lang.Object.wait(Native Method)</p><p>&#160;&#160;&#160; at java.lang.Thread.join(Thread.java:1258)</p><p>&#160;&#160;&#160; at java.lang.Thread.join(Thread.java:1332)</p><p>&#160;&#160;&#160; at org.jboss.mq.SpyMessageConsumer.close(SpyMessageConsumer.java:554)</p><p>&#160;&#160;&#160; at com.tribalfusion.jms.JMSReceiver.closeResources(JMSReceiver.java:88)</p><p>&#160;&#160;&#160; at com.tribalfusion.jms.JMSTopicReceiver.setJMSConfig(JMSTopicReceiver.java:32)</p><p>&#160;&#160;&#160;&#160;&#160; - locked com.tribalfusion.jms.JMSTopicNonDurableSubscriber@600877d4</p><p>&#160;&#160;&#160; at com.tribalfusion.jms.JMSReceiver.reconfigureJMS(JMSReceiver.java:140)</p><p>&#160;&#160;&#160; at com.tribalfusion.jms.JMSReceiver.reconnect(JMSReceiver.java:128)</p><p>&#160;&#160;&#160; at com.tribalfusion.jms.JMSReceiver$2.onException(JMSReceiver.java:77)</p><p>&#160;&#160;&#160; at org.jboss.mq.Connection$ExceptionListenerRunnable.run(Connection.java:1320)</p><p>&#160;&#160;&#160;&#160;&#160; - locked java.lang.Object@4bf3308d</p><p>&#160;&#160;&#160; at java.lang.Thread.run(Thread.java:722)</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><table><tbody><tr><td style=";">MessageListenerThread - dbSaveAckTopic"Id=45 in BLOCKED on lock=java.lang.Object@4bf3308d</td><td style=";"> owned by ExceptionListener Connection@206865307[token=ConnectionToken:ID:44/ee177c7bd4386f174c32bc563cb9b67a rcvstate=STARTED] Id=331</td></tr><tr><td style=";"></td><td style=";">at org.jboss.mq.Connection.asynchFailure(Connection.java:424)</td></tr><tr><td style=";"></td><td style=";">at org.jboss.mq.SpySession.asynchFailure(SpySession.java:1063)</td></tr><tr><td style=";"></td><td style=";">at org.jboss.mq.SpyMessageConsumer.run(SpyMessageConsumer.java:731)</td></tr><tr><td style=";"></td><td style=";">at java.lang.Thread.run(Thread.java:722)</td></tr></tbody></table><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Full thread dump is attached.</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="https://community.jboss.org/message/773764#773764">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in JBoss Messaging at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2042">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>