From a97f665629408c0d812bdc4c70ca5472609f8de7 Mon Sep 17 00:00:00 2001 From: Max Sherman Date: Fri, 10 Feb 2017 09:28:23 -0800 Subject: [PATCH] Guard against small window where sending a message can crash the packager Reviewed By: davidaurelio Differential Revision: D4541015 fbshipit-source-id: 025039c593f7f0f6e40f5fd38ccc07e50f7d04b0 --- local-cli/server/util/inspectorProxy.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/local-cli/server/util/inspectorProxy.js b/local-cli/server/util/inspectorProxy.js index 778327f6c..06570fcb3 100644 --- a/local-cli/server/util/inspectorProxy.js +++ b/local-cli/server/util/inspectorProxy.js @@ -173,7 +173,15 @@ class Device { _send(message: Event) { debug('-> device', this._id, message); - this._socket.send(JSON.stringify(message)); + // This try/catch is unfortunate, but there is a small window where a message can be sent + // 1. after the socket is closed, and + // 2. before the callback for the 'close' event on the socket is run. + // Since we don't want the packager to crash in this situation, we have to guard against this. + try { + this._socket.send(JSON.stringify(message)); + } catch (err) { + debug('Error sending', err); + } } _onMessage(json: string) {