From e456c501998e245428911f9b5a7f9c6320fbc4e8 Mon Sep 17 00:00:00 2001 From: Daniel McNally Date: Fri, 23 Nov 2018 01:18:50 -0500 Subject: [PATCH] fix(p2p): cancel retry connection timer This properly cancels the timer to retry a connection to a peer when the peer is closed. Not canceling the timer could interfere with gracefully shutting down `xud`, as the process would continue waiting for the timer after. Fixes #699. --- lib/p2p/Peer.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/p2p/Peer.ts b/lib/p2p/Peer.ts index c45aa8c2c..1232f0b25 100644 --- a/lib/p2p/Peer.ts +++ b/lib/p2p/Peer.ts @@ -50,6 +50,8 @@ class Peer extends EventEmitter { private socket?: Socket; private parser: Parser = new Parser(Packet.PROTOCOL_DELIMITER); private closed = false; + /** Timer to retry connection to peer after the previous attempt failed. */ + private retryConnectionTimer?: NodeJS.Timer; private connectTimeout?: NodeJS.Timer; private stallTimer?: NodeJS.Timer; private pingTimer?: NodeJS.Timer; @@ -206,6 +208,11 @@ class Peer extends EventEmitter { delete this.socket; } + if (this.retryConnectionTimer) { + clearTimeout(this.retryConnectionTimer); + this.retryConnectionTimer = undefined; + } + if (this.pingTimer) { clearInterval(this.pingTimer); this.pingTimer = undefined; @@ -290,6 +297,7 @@ class Peer extends EventEmitter { } this.socket!.removeListener('error', onError); this.socket!.removeListener('connect', onConnect); + this.retryConnectionTimer = undefined; }; const onConnect = () => { @@ -326,7 +334,7 @@ class Peer extends EventEmitter { `failed: ${err.message}. retrying in ${retryDelay / 1000} sec...`, ); - setTimeout(() => { + this.retryConnectionTimer = setTimeout(() => { retryDelay = Math.min(Peer.CONNECTION_RETRIES_MAX_DELAY, retryDelay * 2); retries = retries + 1; this.socket!.connect(this.address);