Skip to content

Commit

Permalink
fix(p2p): don't reconnect peers when pool closed (#1965)
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
sangaman authored Oct 28, 2020
1 parent 41d103c commit 8483e1e
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/p2p/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,11 @@ class Pool extends EventEmitter {
throw errors.ATTEMPTED_CONNECTION_TO_SELF;
}

if (this.disconnecting || !this.connected) {
// if we are disconnected or disconnecting, don't make new connections to peers
throw errors.POOL_CLOSED;
}

// check if we allow connections to tor addresses
if (!this.config.tor && address.host.indexOf('.onion') !== -1) {
throw errors.NODE_TOR_ADDRESS(nodePubKey, address);
Expand Down Expand Up @@ -972,14 +977,19 @@ class Pool extends EventEmitter {
peer.active = false;
this.emit('peer.close', peer.nodePubKey);

const shouldReconnect =
const doesDisconnectionReasonCallForReconnection =
(peer.sentDisconnectionReason === undefined || peer.sentDisconnectionReason === DisconnectionReason.ResponseStalling) &&
(peer.recvDisconnectionReason === undefined || peer.recvDisconnectionReason === DisconnectionReason.ResponseStalling ||
peer.recvDisconnectionReason === DisconnectionReason.AlreadyConnected ||
peer.recvDisconnectionReason === DisconnectionReason.Shutdown);
const addresses = peer.addresses || [];

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

0 comments on commit 8483e1e

Please sign in to comment.