From b8c568e3e44cd2cf8a00c713ad083d4ce8f75770 Mon Sep 17 00:00:00 2001 From: Pascal Hartig Date: Tue, 7 Aug 2018 06:03:02 -0700 Subject: [PATCH] Fix avd device name resolution with GNU netcat Summary: Device connection didn't work on Linux due to netcat behaving differently. I also played around with the `nc` and `node-netcat` packages, but they would either crash the node stack or fail to compile with Babel. Oh glorious JavaScript world. Reviewed By: danielbuechele Differential Revision: D9194591 fbshipit-source-id: 58e5b8d6b4a66e791e750de2f1449dcb8fc338ae --- src/dispatcher/androidDevice.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/dispatcher/androidDevice.js b/src/dispatcher/androidDevice.js index 950d847df..a6acb7984 100644 --- a/src/dispatcher/androidDevice.js +++ b/src/dispatcher/androidDevice.js @@ -34,10 +34,15 @@ function createDecive(client, device): Promise { function getRunningEmulatorName(id: string): Promise { return new Promise((resolve, reject) => { const port = id.replace('emulator-', ''); + // The GNU version of netcat doesn't terminate after 1s when + // specifying `-w 1`, so we kill it after a timeout. Because + // of that, even in case of an error, there may still be + // relevant data for us to parse. child_process.exec( `echo "avd name" | nc -w 1 localhost ${port}`, - (error: ?Error, data: ?string) => { - if (error == null && data != null) { + {timeout: 1000, encoding: 'utf-8'}, + (error: ?Error, data) => { + if (data != null && typeof data === 'string') { const match = data.trim().match(/(.*)\r\nOK$/); resolve(match != null && match.length > 0 ? match[1] : null); } else {