Skip to content

Commit f312fa5

Browse files
committed
Make AMQConnection#doFinalShutdown idempotent
Now recovery can be triggered from write operations, late connection failure discoveries can re-trigger the shutdown and emit spurious exception. References #341
1 parent 98d32d4 commit f312fa5

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/main/java/com/rabbitmq/client/impl/AMQConnection.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.net.SocketTimeoutException;
3333
import java.util.*;
3434
import java.util.concurrent.*;
35+
import java.util.concurrent.atomic.AtomicBoolean;
3536

3637
final class Copyright {
3738
final static String COPYRIGHT="Copyright (c) 2007-2017 Pivotal Software, Inc.";
@@ -63,6 +64,8 @@ public class AMQConnection extends ShutdownNotifierComponent implements Connecti
6364

6465
private final ErrorOnWriteListener errorOnWriteListener;
6566

67+
private final AtomicBoolean finalShutdownStarted = new AtomicBoolean(false);
68+
6669
/**
6770
* Retrieve a copy of the default table of client properties that
6871
* will be sent to the server during connection startup. This
@@ -700,14 +703,16 @@ private void handleFailure(Throwable ex) {
700703

701704
/** private API */
702705
public void doFinalShutdown() {
703-
_frameHandler.close();
704-
_appContinuation.set(null);
705-
notifyListeners();
706-
// assuming that shutdown listeners do not do anything
707-
// asynchronously, e.g. start new threads, this effectively
708-
// guarantees that we only begin recovery when all shutdown
709-
// listeners have executed
710-
notifyRecoveryCanBeginListeners();
706+
if (finalShutdownStarted.compareAndSet(false, true)) {
707+
_frameHandler.close();
708+
_appContinuation.set(null);
709+
notifyListeners();
710+
// assuming that shutdown listeners do not do anything
711+
// asynchronously, e.g. start new threads, this effectively
712+
// guarantees that we only begin recovery when all shutdown
713+
// listeners have executed
714+
notifyRecoveryCanBeginListeners();
715+
}
711716
}
712717

713718
private void notifyRecoveryCanBeginListeners() {

0 commit comments

Comments
 (0)