From dd536b9d1aac0a73eb1d3f07060641d5acc04e00 Mon Sep 17 00:00:00 2001 From: Pascal Hartig Date: Tue, 10 Aug 2021 16:10:57 -0700 Subject: [PATCH] Gracefully handle socket closures Summary: Got my first auto-created crashbot task. {emoji:1f973} We do have some pretty granular handling for errors during disconnects but this one has fallen through the cracks. I'm not 100% sure if this is the right way to handle it which is why I added mweststrate. :) Reviewed By: mweststrate Differential Revision: D30218833 fbshipit-source-id: 2b4c9201ee7faf1c278b1cc5268ad2648dc4c820 --- desktop/app/src/Client.tsx | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/desktop/app/src/Client.tsx b/desktop/app/src/Client.tsx index 4da625bde..f8d32c06e 100644 --- a/desktop/app/src/Client.tsx +++ b/desktop/app/src/Client.tsx @@ -710,11 +710,22 @@ export default class Client extends EventEmitter { api, method, params, - }).catch((err) => { + }).catch((err: Error) => { // We only throw errors if the connection is still alive // as connection-related ones aren't recoverable from // user code. if (this.connected.get()) { + // This is a special case where we a send failed because of + // a disconnect "mid-air". This can happen, for instance, + // when you pull the plug from a connected phone. We can + // still handle this gracefully. + if (err.toString().includes('Socket closed unexpectedly')) { + console.warn( + `Failed to call device due to unexpected disconnect: ${err}`, + ); + this.disconnect(); + return {}; + } throw err; } // This effectively preserves the previous behavior