Skip to content

Commit

Permalink
fix(p2p): remove socket listeners after destroy
Browse files Browse the repository at this point in the history
This ensures we don't remove listeners from the peer TCP socket before
the socket is destroyed. This prevents edge case errors where a socket
emits an error in the split second before it is destroyed.
  • Loading branch information
sangaman committed Aug 4, 2020
1 parent b219b26 commit 8ec4d45
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/p2p/Peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ class Peer extends EventEmitter {
this.status = PeerStatus.Closed;

if (this.socket) {
this.socket.removeAllListeners();
if (!this.socket.destroyed) {
if (reason !== undefined) {
this.logger.debug(`Peer ${this.label}: closing socket. reason: ${DisconnectionReason[reason]}`);
Expand All @@ -349,6 +348,7 @@ class Peer extends EventEmitter {
}

this.socket.destroy();
this.socket.removeAllListeners();
}
delete this.socket;
}
Expand Down Expand Up @@ -555,7 +555,7 @@ class Peer extends EventEmitter {
} else {
this.socket = net.connect(this.address.port, this.address.host);
this.socket.once('connect', onConnect);
this.socket.once('error', onError);
this.socket.on('error', onError);
}
};

Expand Down

0 comments on commit 8ec4d45

Please sign in to comment.