Skip to content

Commit 8483e1e

Browse files
authored
fix(p2p): don't reconnect peers when pool closed (#1965)
This ensures that we don't attempt to reconnect to peers that have disconnected from us after we have started closing the p2p pool. This may help prevent scenarios where we unintentionally attempt to reconnect to peers after shutting down xud. > Should be tested against [#1668 (comment)](#1668 (comment)) @raladev re-connection after shutdown is disappeared, but my xud still can not be gracefully terminated, it waits something: ``` 28/10/2020 05:17:43.164 [CONNEXT] trace: sending request to /balance/0x69C3d485623bA3f382Fc0FB6756c4574d43C1618 ^C28/10/2020 05:17:44.087 [GLOBAL] info: XUD is shutting down 28/10/2020 05:17:44.088 [LND-BTC] info: new status: Disconnected 28/10/2020 05:17:44.089 [LND-LTC] info: new status: Disconnected 28/10/2020 05:17:44.090 [CONNEXT] info: new status: Disconnected 28/10/2020 05:17:44.093 [P2P] debug: Peer 03ece33a30db1dbce4b62fa96a5e9541138a24997ef5672eebed2d332270e39542 (OzoneYellow): closing socket. reason: Shutdown 28/10/2020 05:17:44.095 [HTTP] info: http server has closed 28/10/2020 05:17:44.096 [RPC] info: gRPC server completed shutdown 28/10/2020 05:17:44.097 [P2P] trace: Sent Disconnecting packet to 03ece33a30db1dbce4b62fa96a5e9541138a24997ef5672eebed2d332270e39542 (OzoneYellow): "{\"body\":{\"reason\":9},\"header\":{\"id\":\"95133be0-1917-11eb-b75b-73d0f0278756\"}}" 28/10/2020 05:17:44.109 [ORDERBOOK] debug: removed all orders for peer 03ece33a30db1dbce4b62fa96a5e9541138a24997ef5672eebed2d332270e39542 (OzoneYellow) 28/10/2020 05:17:44.118 [GLOBAL] info: XUD shutdown gracefully ```
1 parent 41d103c commit 8483e1e

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Diff for: lib/p2p/Pool.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,11 @@ class Pool extends EventEmitter {
431431
throw errors.ATTEMPTED_CONNECTION_TO_SELF;
432432
}
433433

434+
if (this.disconnecting || !this.connected) {
435+
// if we are disconnected or disconnecting, don't make new connections to peers
436+
throw errors.POOL_CLOSED;
437+
}
438+
434439
// check if we allow connections to tor addresses
435440
if (!this.config.tor && address.host.indexOf('.onion') !== -1) {
436441
throw errors.NODE_TOR_ADDRESS(nodePubKey, address);
@@ -972,14 +977,19 @@ class Pool extends EventEmitter {
972977
peer.active = false;
973978
this.emit('peer.close', peer.nodePubKey);
974979

975-
const shouldReconnect =
980+
const doesDisconnectionReasonCallForReconnection =
976981
(peer.sentDisconnectionReason === undefined || peer.sentDisconnectionReason === DisconnectionReason.ResponseStalling) &&
977982
(peer.recvDisconnectionReason === undefined || peer.recvDisconnectionReason === DisconnectionReason.ResponseStalling ||
978983
peer.recvDisconnectionReason === DisconnectionReason.AlreadyConnected ||
979984
peer.recvDisconnectionReason === DisconnectionReason.Shutdown);
980985
const addresses = peer.addresses || [];
981986

982-
if (!peer.inbound && peer.nodePubKey && shouldReconnect && (addresses.length || peer.address)) {
987+
if (doesDisconnectionReasonCallForReconnection
988+
&& !peer.inbound // we don't make reconnection attempts to peers that connected to use
989+
&& peer.nodePubKey // we only reconnect if we know the peer's node pubkey
990+
&& (addresses.length || peer.address) // we only reconnect if there's an address to connect to
991+
&& !this.disconnecting && this.connected // we don't reconnect if we're in the process of disconnecting or have disconnected the p2p pool
992+
) {
983993
this.logger.debug(`attempting to reconnect to a disconnected peer ${peer.label}`);
984994
const node = { addresses, lastAddress: peer.address, nodePubKey: peer.nodePubKey };
985995
await this.tryConnectNode(node, true);

0 commit comments

Comments
 (0)