Skip to content

Commit

Permalink
fix(p2p): handle multiple socket errors
Browse files Browse the repository at this point in the history
This changes the peer socket error handling to use `on` instead of `once`
when listening for `error` events to ensure that an unexpected second
`error` event doesn't crash xud after the `once` handler stops listening.
Instead we use `on` to listen to all error events and then manually
remove all listeners from the socket after it's destroyed.

Fixes #1773
  • Loading branch information
sangaman committed Aug 3, 2020
1 parent 87687a5 commit 716f5d3
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/p2p/Peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ 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 Down Expand Up @@ -724,11 +725,11 @@ class Peer extends EventEmitter {
private bindSocket = () => {
assert(this.socket);

this.socket.once('error', (err) => {
this.socket.on('error', (err) => {
this.logger.error(`Peer (${this.label}) error`, err);
});

this.socket.once('close', async (hadError) => {
this.socket.on('close', async (hadError) => {
// emitted once the socket is fully closed
if (this.nodePubKey === undefined) {
this.logger.info(`Socket closed prior to handshake with ${this.label}`);
Expand Down

0 comments on commit 716f5d3

Please sign in to comment.