From 6a76d56ed3ce3239793b34e463054660129f9111 Mon Sep 17 00:00:00 2001 From: David Lutzardo Date: Thu, 14 Dec 2023 03:01:52 +0100 Subject: [PATCH] Execute the terminatedImmediately method only once (#6284) Signed-off-by: David Lutzardo Co-authored-by: Sally MacFarlane Signed-off-by: jflo --- .../connections/AbstractPeerConnection.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/AbstractPeerConnection.java b/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/AbstractPeerConnection.java index 066f9122cbc..a05d03c7b54 100644 --- a/ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/connections/AbstractPeerConnection.java +++ b/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 agreedCapabilities; private final Map protocolToCapability = new HashMap<>(); private final AtomicBoolean disconnected = new AtomicBoolean(false); + private final AtomicBoolean terminatedImmediately = new AtomicBoolean(false); protected final PeerConnectionEventDispatcher connectionEventDispatcher; private final LabelledMetric outboundMessagesCounter; private final long initiatedAt; @@ -162,17 +163,19 @@ public Set 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();