diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2019-08-22 16:29:04 +0200 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2019-08-22 16:29:04 +0200 |
| commit | a4d3639562ab882dfcb854b7494c7fd39a9022ba (patch) | |
| tree | eb4eb19f488a395aeff26387c654b9a1839f1a75 | |
| parent | ca85d381bd42da1a1e638b7e1bcb45458d566881 (diff) | |
| parent | d376931a7c127269fb3627f6b4143863dc6f7557 (diff) | |
| download | mullvadvpn-a4d3639562ab882dfcb854b7494c7fd39a9022ba.tar.xz mullvadvpn-a4d3639562ab882dfcb854b7494c7fd39a9022ba.zip | |
Merge branch 'fix-socket-connection-double-close-call'
| -rw-r--r-- | gui/src/main/jsonrpc-client.ts | 45 |
1 files changed, 27 insertions, 18 deletions
diff --git a/gui/src/main/jsonrpc-client.ts b/gui/src/main/jsonrpc-client.ts index 34e4797442..f3ac7e78e1 100644 --- a/gui/src/main/jsonrpc-client.ts +++ b/gui/src/main/jsonrpc-client.ts @@ -349,25 +349,8 @@ export class SocketTransport implements ITransport<{ path: string }> { if (this.connection) { log.debug('Close socket'); - // closing socket is not synchronous, so remove all of the event handlers first - this.connection - .removeListener('ready', this.onSocketReady) - .removeListener('error', this.onSocketError) - .removeListener('close', this.onSocketClose); + this.cleanupConnection(true); - this.jsonStream!.removeListener('data', this.onJsonStreamData).removeListener( - 'error', - this.onJsonStreamError, - ); - - try { - this.connection.end(); - } catch (error) { - log.error('Failed to close the socket: ', error); - } - - this.connection = undefined; - this.jsonStream = undefined; this.onClose(); } } @@ -395,6 +378,8 @@ export class SocketTransport implements ITransport<{ path: string }> { }; private onSocketClose = (hadError: boolean) => { + this.cleanupConnection(false); + if (hadError) { log.debug(`Socket was closed due to an error: `, this.lastError); @@ -418,4 +403,28 @@ export class SocketTransport implements ITransport<{ path: string }> { this.connection.destroy(error); } }; + + private cleanupConnection(shouldClose: boolean) { + // closing socket is not synchronous, so remove all of the event handlers first + this.connection!.removeListener('ready', this.onSocketReady) + .removeListener('error', this.onSocketError) + .removeListener('close', this.onSocketClose); + + this.jsonStream!.removeListener('data', this.onJsonStreamData).removeListener( + 'error', + this.onJsonStreamError, + ); + + if (shouldClose) { + try { + this.connection!.end(); + } catch (error) { + log.error('Failed to close the socket: ', error); + } + } + + this.connection = undefined; + this.jsonStream = undefined; + this.socketReady = false; + } } |
