Skip to content

Commit c9e79b8

Browse files
pranay-v29claude
andcommitted
LOC-7420: tolerate a busy binary instead of crashing the consumer
On Windows, BrowserStackLocal.exe in ~/.browserstack is routinely unopenable for a moment -- an AV scan of a freshly written executable, a tunnel still releasing its handle, two workers starting at once. POSIX allows opening and unlinking a file in use, so this only shows on Windows. Several defects turned that transient condition into a crash before any session started. 1. download.js and LocalBinary.js registered the write-stream 'error' handler inside the async https.get callback. createWriteStream emits on the next tick, long before that runs, so the error had no listener and node's `throw er` killed the download child. Handlers now attach immediately after createWriteStream. Handling the error is not enough on its own: the request is still in flight, and without destroying it the child keeps downloading into a dead stream while the parent's spawnSync blocks for a full download before it can retry. The throw was also stopping the download. 2. retryBinaryDownload did its work in an async callback, so the sync path returned undefined to a caller that had already given up -- surfacing as "Couldn't find binary file" while the retries ran on, orphaned, in the background. This happened even when the unlink succeeded, so it is not a consequence of the EPERM. 3. Retrying instantly against a live lock just burns the retry budget, so a busy binary is now probed and waited on, bounded, rather than deleted. Follows the CLI binary's existing busy-code handling. 4. spawnSync reports spawn failures via obj.error, leaving stdout null; reading .length threw a TypeError that replaced the real cause. execFile raises the same class of failure synchronously, where an uncaught throw inside the getBinaryPath callback killed the consumer's process outright. Both are handled now. Tests force the open to fail rather than reproducing a lock, since the defect is any createWriteStream failure rather than EBUSY specifically, so they need no Windows runner, network or credentials. .eslintrc.json moves to es2017 for Atomics/SharedArrayBuffer. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 8096a53 commit c9e79b8

5 files changed

Lines changed: 240 additions & 62 deletions

File tree

‎.eslintrc.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"no-constant-condition": 0
2121
},
2222
"env": {
23-
"es6": true,
23+
"es2017": true,
2424
"node": true
2525
},
2626
"extends": "eslint:recommended",

‎lib/Local.js‎

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ function Local(){
5858
}
5959
try{
6060
const obj = childProcess.spawnSync(that.binaryPath, that.getBinaryArgs());
61+
/* stdout is null on a spawn failure; reading .length masked the real cause. */
62+
if(obj.error) {
63+
throw obj.error;
64+
}
6165
this.tunnel = {pid: obj.pid};
6266
var data = {};
6367
if(obj.stdout.length > 0)
@@ -79,7 +83,8 @@ function Local(){
7983
if(that.retriesLeft > 0) {
8084
console.log('Retrying Binary Download. Retries Left', that.retriesLeft);
8185
that.retriesLeft -= 1;
82-
fs.unlinkSync(that.binaryPath);
86+
/* EPERM on a locked file threw straight out of startSync. */
87+
try { fs.unlinkSync(that.binaryPath); } catch(err) { /* ignored */ }
8388
delete(that.binaryPath);
8489
that.binaryDownloadState.errorMessage = binaryDownloadErrorMessage;
8590
that.binaryDownloadState.fallbackEnabled = true;
@@ -107,46 +112,55 @@ function Local(){
107112
}
108113

109114
that.opcode = 'start';
110-
that.tunnel = childProcess.execFile(that.binaryPath, that.getBinaryArgs(), function(error, stdout, stderr){
111-
if(error) {
112-
const binaryDownloadErrorMessage = `Error while trying to execute binary: ${util.format(error)}`;
113-
console.error(binaryDownloadErrorMessage);
114-
if(that.retriesLeft > 0) {
115-
console.log('Retrying Binary Download. Retries Left', that.retriesLeft);
116-
that.retriesLeft -= 1;
117-
fs.unlinkSync(that.binaryPath);
118-
delete(that.binaryPath);
119-
that.binaryDownloadState.errorMessage = binaryDownloadErrorMessage;
120-
that.binaryDownloadState.fallbackEnabled = true;
121-
that.start(options, callback);
122-
return;
123-
} else {
124-
callback(new LocalError(error.toString()));
125-
return;
126-
}
127-
}
128115

129-
var data = {};
130-
var output = stdout || stderr;
131-
if(!output) {
132-
callback(new LocalError('No output received'));
133-
return;
134-
}
135-
try {
136-
data = JSON.parse(output);
137-
} catch(parseError) {
138-
callback(new LocalError('Invalid output received: ' + parseError.message, output));
116+
var onBinaryError = function(error) {
117+
const binaryDownloadErrorMessage = `Error while trying to execute binary: ${util.format(error)}`;
118+
console.error(binaryDownloadErrorMessage);
119+
if(that.retriesLeft > 0) {
120+
console.log('Retrying Binary Download. Retries Left', that.retriesLeft);
121+
that.retriesLeft -= 1;
122+
try { fs.unlinkSync(that.binaryPath); } catch(err) { /* ignored */ }
123+
delete(that.binaryPath);
124+
that.binaryDownloadState.errorMessage = binaryDownloadErrorMessage;
125+
that.binaryDownloadState.fallbackEnabled = true;
126+
that.start(options, callback);
139127
return;
140128
}
129+
callback(new LocalError(error.toString()));
130+
};
141131

142-
if(data['state'] != 'connected'){
143-
callback(new LocalError(that.getErrorMessage(data)));
144-
} else {
145-
that.pid = data['pid'];
146-
that.isProcessRunning = true;
147-
callback();
148-
}
149-
});
132+
/* execFile raises spawn failures synchronously, not via its callback, and
133+
an uncaught throw inside this callback kills the consumer's process. */
134+
try {
135+
that.tunnel = childProcess.execFile(that.binaryPath, that.getBinaryArgs(), function(error, stdout, stderr){
136+
if(error) {
137+
return onBinaryError(error);
138+
}
139+
140+
var data = {};
141+
var output = stdout || stderr;
142+
if(!output) {
143+
callback(new LocalError('No output received'));
144+
return;
145+
}
146+
try {
147+
data = JSON.parse(output);
148+
} catch(parseError) {
149+
callback(new LocalError('Invalid output received: ' + parseError.message, output));
150+
return;
151+
}
152+
153+
if(data['state'] != 'connected'){
154+
callback(new LocalError(that.getErrorMessage(data)));
155+
} else {
156+
that.pid = data['pid'];
157+
that.isProcessRunning = true;
158+
callback();
159+
}
160+
});
161+
} catch(spawnError) {
162+
onBinaryError(spawnError);
163+
}
150164
}, options['bs-host']);
151165
};
152166

‎lib/LocalBinary.js‎

Lines changed: 71 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ function LocalBinary(){
7171
env.BROWSERSTACK_LOCAL_AUTH_TOKEN = this.key;
7272
}
7373
const obj = childProcess.spawnSync(cmd, opts, { env: env });
74+
/* stdout is null on a spawn failure; reading .length masked the real cause. */
75+
if(obj.error) {
76+
throw(util.format(obj.error));
77+
}
7478
if(obj.stdout.length > 0) {
7579
this.sourceURL = obj.stdout.toString().replace(/\n+$/, '');
7680
this.downloadState.sourceURL = this.sourceURL;
@@ -148,23 +152,57 @@ function LocalBinary(){
148152
this.downloadErrorMessage = errorMessagePrefix + ' : ' + errorMessage;
149153
};
150154

155+
/* A locked binary is transient on Windows (AV scan, a tunnel still releasing
156+
its handle), not a corrupt one. Mirrors the CLI binary's existing probe. */
157+
this.BUSY_ERROR_CODES = ['EBUSY', 'EPERM', 'ETXTBSY', 'EACCES'];
158+
this.BUSY_MAX_WAITS = 3;
159+
this.BUSY_WAIT_MS = 1000;
160+
161+
this.isBinaryBusy = function(binaryPath) {
162+
try {
163+
fs.closeSync(fs.openSync(binaryPath, 'r+'));
164+
return false;
165+
} catch(err) {
166+
return this.BUSY_ERROR_CODES.indexOf(err.code) !== -1;
167+
}
168+
};
169+
170+
/* Blocking by design: the sync path has no event loop to come back to. */
171+
this.waitWhileBinaryBusySync = function(binaryPath) {
172+
for(var i = 0; i < this.BUSY_MAX_WAITS; i++) {
173+
if(!fs.existsSync(binaryPath) || !this.isBinaryBusy(binaryPath)) return;
174+
console.log('Binary is in use, waiting before retrying.');
175+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, this.BUSY_WAIT_MS);
176+
}
177+
};
178+
151179
this.retryBinaryDownload = function(conf, destParentDir, callback, retries, binaryPath) {
152180
var that = this;
153-
if(retries > 0) {
154-
console.log('Retrying Download. Retries left', retries);
155-
/* Single unlink instead of stat-then-unlinkSync: the gap between the two
156-
let a concurrent writer swap the file, and a failing unlinkSync threw
157-
out of the stat callback where it could not be caught. A missing file
158-
is the expected case here, so any error is ignored. */
181+
if(retries <= 0) {
182+
console.error('Number of retries to download exceeded.');
183+
return;
184+
}
185+
console.log('Retrying Download. Retries left', retries);
186+
187+
/* Must stay synchronous: this return value is what downloadSync ->
188+
binaryPath() -> Local.getBinaryPath hands back. Retrying inside a callback
189+
returned undefined before the retry had done anything. */
190+
if(!callback) {
191+
that.waitWhileBinaryBusySync(binaryPath);
192+
try { fs.unlinkSync(binaryPath); } catch(err) { /* missing or locked */ }
193+
return that.downloadSync(conf, destParentDir, retries - 1);
194+
}
195+
196+
var attemptAsync = function(waitsLeft) {
197+
if(waitsLeft > 0 && fs.existsSync(binaryPath) && that.isBinaryBusy(binaryPath)) {
198+
console.log('Binary is in use, waiting before retrying.');
199+
return setTimeout(function() { attemptAsync(waitsLeft - 1); }, that.BUSY_WAIT_MS);
200+
}
159201
fs.unlink(binaryPath, function() {
160-
if(!callback) {
161-
return that.downloadSync(conf, destParentDir, retries - 1);
162-
}
163202
that.download(conf, destParentDir, callback, retries - 1);
164203
});
165-
} else {
166-
console.error('Number of retries to download exceeded.');
167-
}
204+
};
205+
attemptAsync(that.BUSY_MAX_WAITS);
168206
};
169207

170208
this.downloadSync = function(conf, destParentDir, retries) {
@@ -198,6 +236,10 @@ function LocalBinary(){
198236
const userAgent = [packageName, version].join('/');
199237
const env = Object.assign({ 'USER_AGENT': userAgent }, process.env);
200238
const obj = childProcess.spawnSync(cmd, opts, { env: env });
239+
if(obj.error) {
240+
that.binaryDownloadError('Download failed with error', util.format(obj.error));
241+
return that.retryBinaryDownload(conf, destParentDir, null, retries, binaryPath);
242+
}
201243
let output;
202244
if(obj.stdout.length > 0) {
203245
if(fs.existsSync(binaryPath)){
@@ -234,6 +276,21 @@ function LocalBinary(){
234276
var binaryPath = path.join(destParentDir, destBinaryName);
235277
var fileStream = fs.createWriteStream(binaryPath);
236278

279+
/* A failed open and the in-flight request can both report on the same
280+
attempt; one attempt must trigger at most one retry. */
281+
var retried = false;
282+
var retryOnce = function(prefix, err) {
283+
that.binaryDownloadError(prefix, util.format(err));
284+
if(retried) return;
285+
retried = true;
286+
that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath);
287+
};
288+
289+
/* Same as lib/download.js: the open() failure lands first. */
290+
fileStream.on('error', function (err) {
291+
retryOnce('Got Error while downloading binary file', err);
292+
});
293+
237294
var options = url.parse(this.httpPath);
238295
if(conf.proxyHost && conf.proxyPort) {
239296
options.agent = new HttpsProxyAgent({
@@ -267,21 +324,15 @@ function LocalBinary(){
267324
}
268325

269326
response.on('error', function(err) {
270-
that.binaryDownloadError('Got Error in binary download response', util.format(err));
271-
that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath);
272-
});
273-
fileStream.on('error', function (err) {
274-
that.binaryDownloadError('Got Error while downloading binary file', util.format(err));
275-
that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath);
327+
retryOnce('Got Error in binary download response', err);
276328
});
277329
fileStream.on('close', function () {
278330
fs.chmod(binaryPath, '0755', function() {
279331
callback(binaryPath);
280332
});
281333
});
282334
}).on('error', function(err) {
283-
that.binaryDownloadError('Got Error in binary downloading request', util.format(err));
284-
that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath);
335+
retryOnce('Got Error in binary downloading request', err);
285336
});
286337
});
287338
};

‎lib/download.js‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ const binaryPath = process.argv[2], httpPath = process.argv[3], proxyHost = proc
99

1010
var fileStream = fs.createWriteStream(binaryPath);
1111

12+
/* Must be attached before the async https.get: createWriteStream emits 'error'
13+
on the next tick, and with no listener node turns that into a hard throw. */
14+
var request;
15+
16+
fileStream.on('error', function (err) {
17+
console.error('Got Error while downloading binary file', err);
18+
process.exitCode = 1;
19+
/* Otherwise the child keeps downloading into a dead stream and the parent's
20+
spawnSync blocks for a whole download before it can retry. */
21+
if(request) request.destroy();
22+
});
23+
1224
var options = url.parse(httpPath);
1325
/* isUndefined, not plain truthiness: the parent passes literal `undefined`
1426
placeholders for the proxy slots when only a CA is configured, and those
@@ -37,7 +49,7 @@ options.headers = Object.assign({}, options.headers, {
3749
'user-agent': process.env.USER_AGENT,
3850
});
3951

40-
https.get(options, function (response) {
52+
request = https.get(options, function (response) {
4153
const contentEncoding = response.headers['content-encoding'];
4254
if (typeof contentEncoding === 'string' && contentEncoding.match(/gzip/i)) {
4355
if (process.env.BROWSERSTACK_LOCAL_DEBUG_GZIP) {
@@ -52,12 +64,10 @@ https.get(options, function (response) {
5264
response.on('error', function(err) {
5365
console.error('Got Error in binary download response', err);
5466
});
55-
fileStream.on('error', function (err) {
56-
console.error('Got Error while downloading binary file', err);
57-
});
5867
fileStream.on('close', function () {
5968
console.log('Done');
6069
});
6170
}).on('error', function(err) {
71+
if(process.exitCode === 1) return; // our own destroy() landing
6272
console.error('Got Error in binary downloading request', err);
6373
});

0 commit comments

Comments
 (0)