Recovery connection leak
The AutorecoveringConnection.java method seems to leak connection:
in this code we create new connection:
private RecoveryAwareAMQConnection recoverConnection() throws InterruptedException {
while (!manuallyClosed)
{
try {
RecoveryAwareAMQConnection newConn = this.cf.newConnection();
synchronized(recoveryLock) {
if (!manuallyClosed) {
// This is the standard case.
return newConn;
}
}
// This is the once in a blue moon case.
// Application code just called close as the connection
// was being re-established. So we attempt to close the newly created connection.
newConn.abort();
return null;
} catch (Exception e) {
// TODO: exponential back-off
Thread.sleep(this.params.getNetworkRecoveryInterval());
this.getExceptionHandler().handleConnectionRecoveryException(this, e);
}
}
return null;
}
the calling code is:
synchronized private void beginAutomaticRecovery() throws InterruptedException {
Thread.sleep(this.params.getNetworkRecoveryInterval());
this.notifyRecoveryListenersStarted();
**final RecoveryAwareAMQConnection newConn = this.recoverConnection();**
if (newConn == null) {
return;
}
this.addAutomaticRecoveryListener(newConn);
this.recoverShutdownListeners(newConn);
this.recoverBlockedListeners(newConn);
this.recoverChannels(newConn);
_**// don't assign new delegate connection until channel recovery is complete
this.delegate = newConn;**_
if(this.params.isTopologyRecoveryEnabled()) {
this.recoverEntities();
this.recoverConsumers();
}
this.notifyRecoveryListenersComplete();
}
I think that before assigning the new connection to the delegate the old one should be closed, otherwise, the old one is still there.
So the fix should be:
if (his.delegate != null){
this.delegate.abort();
this.delegate.close();
}
his.delegate = newConn;
Not sure this is necessary, as the underlying connection is supposed to be already closed. We may check with the Connection#isOpen method before forcing closing it (and the call to close should be protected by a try/catch block, to avoid failing the recovery process). WDYT @michaelklishin ?
Did you experience a leak while running you app? If so, could you provide a test that triggers the leak? I guess it would be easily spotted with a profiler.
Yes I see leak. The way to reproduce: Configure the host to unknown host than add it to the hosts file. Remove it again from hosts and stop rabbitmq server, Than add and start the server.
@igreenfield Can you be more specific about the scenario to reproduce (use explicit hostnames)? When do you start the Java application and which host is it configured to connect to?
Also, please attach a screenshot of the profiler you use to identify the leak.
Thanks!
In theory it should not be necessary as delegate connections (AMQConnection) already go through the shutdown process when recovery starts. It is certainly should not be necessary to call both AMQConnection#abort and close, perhaps you only want #abort. I wonder if double closing can result in double invocation of shutdown listeners, something that can produce a resource leak.
Without a way to reproduce and evidence from a profiler run, this seems to be a solution in search of a problem.
@igreenfield please provide a step by step way of reproducing. You should also enable debug logging for the Java client and post both client and server logs. This is mailing list material at the moment.
java.net.UnknownHostException is a subclass of java.io.IOException, therefore it should be handled exactly the same way as most other reconnection failure cases. Inspecting the code doesn't make it obvious how a recovery can start on a connection that is not already closing, in particular if you restart the server.
Guessing is a highly inefficient way of debugging software, so please save Team RabbitMQ some time and provide as much as possible details about how to reproduce this on the mailing list. We won't be able to help you otherwise.
Hi @michaelklishin @acogoluegnes , Here is how to reproduce it:
I configure the Rabbit java client to some host for example: test-machine123.com and in my local windows machine I point it to my local Rabbit in hosts file: 127.0.0.1 test-machine123.com then the app is up and there is 1 connection to Rabbit. Next I remove the line from hosts file and stopped my local Rabbit, this trigger the following exceptions on my client app:
2017/09/07 06:32:14.848 [RabbitMQMessagingFactory] [AMQP Connection 127.0.0.1:5672]: ERROR: FCID:,Connection shutdown detected. Reason: com.rabbitmq.client.ShutdownSignalException: connection error
[Exception: com.rabbitmq.client.ShutdownSignalException: connection error
at com.rabbitmq.client.impl.AMQConnection.startShutdown(AMQConnection.java:868)
at com.rabbitmq.client.impl.AMQConnection.shutdown(AMQConnection.java:858)
at com.rabbitmq.client.impl.AMQConnection.handleFailure(AMQConnection.java:681)
at com.rabbitmq.client.impl.AMQConnection.access$400(AMQConnection.java:47)
at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:582)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
at java.io.DataInputStream.readUnsignedByte(DataInputStream.java:288)
at com.rabbitmq.client.impl.Frame.readFrom(Frame.java:91)
at com.rabbitmq.client.impl.SocketFrameHandler.readFrame(SocketFrameHandler.java:164)
at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:578)
... 1 more
]
And:
2017/09/07 06:32:24.823 [ForgivingExceptionHandler] [AMQP Connection 127.0.0.1:5672]: ERROR: FCID:,Caught an exception during connection recovery!
[Exception: java.net.UnknownHostException: test-machine123.com
at java.net.InetAddress.getAllByName0(InetAddress.java:1280)
at java.net.InetAddress.getAllByName(InetAddress.java:1192)
at java.net.InetAddress.getAllByName(InetAddress.java:1126)
at com.rabbitmq.client.DnsRecordIpAddressResolver.resolveIpAddresses(DnsRecordIpAddressResolver.java:83)
at com.rabbitmq.client.DnsRecordIpAddressResolver.getAddresses(DnsRecordIpAddressResolver.java:73)
at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:56)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.recoverConnection(AutorecoveringConnection.java:531)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.beginAutomaticRecovery(AutorecoveringConnection.java:494)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.access$000(AutorecoveringConnection.java:53)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection$1.recoveryCanBegin(AutorecoveringConnection.java:435)
at com.rabbitmq.client.impl.AMQConnection.notifyRecoveryCanBeginListeners(AMQConnection.java:700)
at com.rabbitmq.client.impl.AMQConnection.doFinalShutdown(AMQConnection.java:694)
at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:584)
at java.lang.Thread.run(Thread.java:745)
]
Next I restored the hosts file to point to my local Rabbit and started my Rabbit. At this point I can see there are 2 connections, after doing it several times I am in a state where there are 41 connections to Rabbit, here is a snapshot of my client app threads from my Intellij debug console:

Also in the server I can see it has 41 connections:

But only 2 are really connected to the queue:

when looking deeper in the leak thread I see:
So this thread never exit the mainLoop....
even so only one should be connected!! seems like the thread of the connection does not stop.