Skip to content

Commit

Permalink
Execute the terminatedImmediately method only once (hyperledger#6284)
Browse files Browse the repository at this point in the history
Signed-off-by: David Lutzardo <[email protected]>
Co-authored-by: Sally MacFarlane <[email protected]>
Signed-off-by: jflo <[email protected]>
  • Loading branch information
2 people authored and jflo committed Dec 18, 2023
1 parent 6d371a2 commit 6a76d56
Showing 1 changed file with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public abstract class AbstractPeerConnection implements PeerConnection {
private final Set<Capability> agreedCapabilities;
private final Map<String, Capability> protocolToCapability = new HashMap<>();
private final AtomicBoolean disconnected = new AtomicBoolean(false);
private final AtomicBoolean terminatedImmediately = new AtomicBoolean(false);
protected final PeerConnectionEventDispatcher connectionEventDispatcher;
private final LabelledMetric<Counter> outboundMessagesCounter;
private final long initiatedAt;
Expand Down Expand Up @@ -162,17 +163,19 @@ public Set<Capability> getAgreedCapabilities() {

@Override
public void terminateConnection(final DisconnectReason reason, final boolean peerInitiated) {
if (disconnected.compareAndSet(false, true)) {
connectionEventDispatcher.dispatchDisconnect(this, reason, peerInitiated);
if (terminatedImmediately.compareAndSet(false, true)) {
if (disconnected.compareAndSet(false, true)) {
connectionEventDispatcher.dispatchDisconnect(this, reason, peerInitiated);
}
// Always ensure the context gets closed immediately even if we previously sent a disconnect
// message and are waiting to close.
closeConnectionImmediately();
LOG.atTrace()
.setMessage("Terminating connection {}, reason {}")
.addArgument(this)
.addArgument(reason)
.log();
}
// Always ensure the context gets closed immediately even if we previously sent a disconnect
// message and are waiting to close.
closeConnectionImmediately();
LOG.atTrace()
.setMessage("Terminating connection {}, reason {}")
.addArgument(this)
.addArgument(reason)
.log();
}

protected abstract void closeConnectionImmediately();
Expand Down

0 comments on commit 6a76d56

Please sign in to comment.