[rabbitmq-discuss] Java client: channel shutdown notification while closing connection

jiri at krutil.com jiri at krutil.com
Tue Jan 11 10:50:28 GMT 2011


Hi

I have run into a deadlock in my Java client application.

I have a synchronized method registered as a shutdown listener on both  
the channel and the connection.

One of our theads got stuck while trying to close the connection  
inside a method synchronized on the same lock as the shutdown listener.

The thread was executing this code:

     private synchronized void closeConnection() throws IOException {
         connection.removeShutdownListener(this);
         if (connection.isOpen())
             connection.close();
     }

The connection shutdown listener has been disabled, but it seems that  
something has triggered the channel shutdown listener. Not sure if  
this was the attempt to close the connection or something else.

The call stack of the closeConnection thread was:

     java/lang/Object.wait(Native Method)
     java/lang/Object.wait(Object.java:199)
     com/rabbitmq/utility/BlockingCell.get(BlockingCell.java:64)
     com/rabbitmq/utility/BlockingCell.get(BlockingCell.java:79)
      
com/rabbitmq/utility/BlockingCell.uninterruptibleGet(BlockingCell.java:125)
      
com/rabbitmq/utility/BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:51)
      
com/rabbitmq/client/impl/AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:348)
     com/rabbitmq/client/impl/AMQConnection.close(AMQConnection.java:710)
     com/rabbitmq/client/impl/AMQConnection.close(AMQConnection.java:640)
     com/rabbitmq/client/impl/AMQConnection.close(AMQConnection.java:626)
     com/rabbitmq/client/impl/AMQConnection.close(AMQConnection.java:619)
     AmqpAdapter.closeConnection(AmqpAdapter.java:477)
     ...

It seems that the method Connection.close() running in our thread was  
waiting for the completion of the channel shutdown listener running in  
the AMQP connection thread. But since the registered listener was  
waiting for the Java lock held by my method closeConnection(), a  
deadlock occured.

Both broker and Java client versions are 2.1.0.

Apparently I am doing something wrong, but I'm not sure what is the  
correct way how to do these things.

Should I unregister the channel shutdown listener before closing the  
connection?
Or should I avoid any synchronization in my shutdown listener? (would  
probably have to redesign)
It seems Connection.close() is synchronous. Should I make my shutdown  
listener asynchronous? (not sure how I would do that)

Any help is appreciated!

Jiri



More information about the rabbitmq-discuss mailing list