activemq-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Stirling Chow (JIRA)" <>
Subject [jira] Created: (AMQ-3185) All VM transports on a broker can be prematurely closed.
Date Sun, 20 Feb 2011 19:16:38 GMT
All VM transports on a broker can be prematurely closed.

                 Key: AMQ-3185
             Project: ActiveMQ
          Issue Type: Bug
          Components: Transport
    Affects Versions: 5.4.2
            Reporter: Stirling Chow
            Priority: Critical
         Attachments: patch.diff

We have eight servers running AMQ 5.3.1 connected in a network-of-brokers over HTTP.  Each
broker maintains local connections to internal consumers using the VM transport.  We were
noticing that about once every day, all the local VM connections on a broker would fail with
the following error:

2010-12-10 04:29:11,663 [processBroker-process-pool-thread-4] ERROR - The worker encountered
an exception and will pause for 5 seconds before continuing.
javax.jms.JMSException: Peer (vm://broker-mbus-200005#1052452) disposed.
	at org.apache.activemq.util.JMSExceptionSupport.create(
	at org.apache.activemq.ActiveMQMessageConsumer.dequeue(
	at org.apache.activemq.ActiveMQMessageConsumer.receive(
	at Source)
Caused by: org.apache.activemq.transport.TransportDisposedIOException: Peer (vm://broker-mbus-200005#1052452)
	at org.apache.activemq.transport.vm.VMTransport.stop(
	at org.apache.activemq.transport.TransportFilter.stop(
	at org.apache.activemq.transport.TransportFilter.stop(
	at org.apache.activemq.transport.ResponseCorrelator.stop(
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$ Source)
	... 1 more

This was quite unexpected since we create the VM transport connection at broker startup and
maintain it (the single connection) throughout the life of the application.  The connection
is only closed when the process terminates and the broker is stopped.

Since we wrote our consumers against the JMS spec, we handle periodic connection failures
by creating a new connection.  This is works fine sometimes; however, because of AMQ-3127,
the sudden reregistration of our consumers simultaneously occurring with bridge creation causes
frequent deadlock that can only be resolved by restarting the systems.

We were unable to reliably recreate the failure, so it became clear that a timing issue was
involved.  Eventually, we determined the cause of the VM transport failure was due to the
following code in VMTransportServer:

    public VMTransport connect() throws IOException {
        TransportAcceptListener al;
        synchronized (this) {
            if (disposed) {
                throw new IOException("Server has been disposed.");
            al = acceptListener;
        if (al == null) {
            throw new IOException("Server TransportAcceptListener is null.");

        VMTransport client = new VMTransport(location) {
            public void stop() throws Exception {
                if (disposed) {
                if (connectionCount.decrementAndGet() == 0 && disposeOnDisconnect)

        VMTransport server = new VMTransport(location);
        return client;

At issue is the override VMTransport.stop() method:

            public void stop() throws Exception {
                if (disposed) {
                if (connectionCount.decrementAndGet() == 0 && disposeOnDisconnect)

Note that VMTransport.disposed is used to protect against multiple calls and subsequently
multiple decrements of connectionCount.  However, in the implementation of super.stop(), the
disposed flag is only set after the peer transport is informed of the stop:

    public void stop() throws Exception {
        // If stop() is called while being start()ed.. then we can't stop until we return
to the start() method.
        if( enqueueValve.isOn() ) {
            // let the peer know that we are disconnecting..
            try {
>>>                peer.transportListener.onCommand(new ShutdownInfo());
            } catch (Exception ignore) {
            TaskRunner tr = null;
            try {
                if (!disposed) {
                    started = false;
>>>                    disposed = true;
                    if (taskRunner != null) {
                        tr = taskRunner;
                        taskRunner = null;
            } finally {
            if (tr != null) {


TransportConnection implements of peer.transportListener.onCommand(new ShutdownInfo()) by
launching a asynchronous task that eventually calls back to the same transport that initiated
the closure.  If the timing is right, VMTransportServer's VMTransport.stop() method is called
a second time before the disposed flag is set to true.  As a result, the connectionCount is
decremented *TWICE* instead of just once.

In other words, the diposed check and decrement as implemented by VMTransport's anonymous
VMTransport subclass are not thread-safe.  If VMTransportServer miscounts the connections,
it can end up stopping itself while there are still live connections.  The result is that
the live connections see their peer (the server part of the VMTransport) unexpectedly closed.

The attached patch prevents multiple decrements of the connectionCount by preventing reentrant
calls to VMTransportServer's VMTransport stop() method.

A patch is included which demonstrates the problem with the existing AMQ trunk code.

This message is automatically generated by JIRA.
For more information on JIRA, see:


View raw message