Execute the terminatedImmediately method only once (#6284)

Signed-off-by: David Lutzardo <jdlutzardo@izertis.com>
Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com>
pull/6297/head
David Lutzardo 11 months ago committed by GitHub
parent 0f8935677b
commit 75df5d60d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/AbstractPeerConnection.java

@ -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;
@ -162,17 +163,19 @@ public abstract class AbstractPeerConnection implements PeerConnection {
@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();

Loading…
Cancel
Save