Skip to content

Commit 69b1a39

Browse files
pranay-v29claude
andcommitted
Reuse a running binary instead of replacing it
Parallel runs on one machine share ~/.browserstack/BrowserStackLocal.exe. While one run is executing it, Windows keeps the file locked for that run's whole lifetime, so a second run that reaches the download path hits EBUSY on the open. 1.5.15 no longer crashes on that, but it waits three seconds and then tries to delete and re-download the file -- which cannot succeed while the first run holds it, so the second run exhausts its retries and never gets a tunnel (SDK-7713). A locked binary that still answers --version is a working binary. Before waiting on or replacing it, probe it and reuse it if it runs; probe again after the wait, since an AV scan or a finishing writer can release it. A file that does not run is still replaced as before. The version check mirrors browserstack-local-python's __verify_binary. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1e479a6 commit 69b1a39

2 files changed

Lines changed: 71 additions & 6 deletions

File tree

‎lib/LocalBinary.js‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var https = require('https'),
1414
fetchDownloadSourceUrlAsync = require('./fetchDownloadSourceUrlAsync');
1515

1616
const packageName = 'browserstack-local-nodejs';
17+
const VERSION_REGEX = /BrowserStack Local version \d+\.\d+/;
1718

1819
function LocalBinary(){
1920
this.hostOS = process.platform;
@@ -178,6 +179,24 @@ function LocalBinary(){
178179
}
179180
};
180181

182+
/* A binary that answers --version is usable as-is, even while another run holds it. */
183+
this.isUsableBinarySync = function(binaryPath) {
184+
if(!fs.existsSync(binaryPath)) return false;
185+
var obj = childProcess.spawnSync(binaryPath, ['--version'], { timeout: 10000 });
186+
return !obj.error && VERSION_REGEX.test(String(obj.stdout));
187+
};
188+
189+
this.isUsableBinary = function(binaryPath, done) {
190+
if(!fs.existsSync(binaryPath)) return done(false);
191+
try {
192+
childProcess.execFile(binaryPath, ['--version'], { timeout: 10000 }, function(err, stdout) {
193+
done(!err && VERSION_REGEX.test(String(stdout)));
194+
});
195+
} catch(err) {
196+
done(false);
197+
}
198+
};
199+
181200
this.retryBinaryDownload = function(conf, destParentDir, callback, retries, binaryPath) {
182201
var that = this;
183202
if(retries <= 0) {
@@ -193,18 +212,23 @@ function LocalBinary(){
193212
binaryPath() -> Local.getBinaryPath hands back. Retrying inside a callback
194213
returned undefined before the retry had done anything. */
195214
if(!callback) {
215+
if(that.isUsableBinarySync(binaryPath)) return binaryPath;
196216
that.waitWhileBinaryBusySync(binaryPath);
217+
if(that.isUsableBinarySync(binaryPath)) return binaryPath;
197218
try { fs.unlinkSync(binaryPath); } catch(err) { /* missing or locked */ }
198219
return that.downloadSync(conf, destParentDir, retries - 1);
199220
}
200221

201222
var attemptAsync = function(waitsLeft) {
202-
if(waitsLeft > 0 && fs.existsSync(binaryPath) && that.isBinaryBusy(binaryPath)) {
203-
console.log('Binary is in use, waiting before retrying.');
204-
return setTimeout(function() { attemptAsync(waitsLeft - 1); }, that.BUSY_WAIT_MS);
205-
}
206-
fs.unlink(binaryPath, function() {
207-
that.download(conf, destParentDir, callback, retries - 1);
223+
that.isUsableBinary(binaryPath, function(usable) {
224+
if(usable) return callback(binaryPath);
225+
if(waitsLeft > 0 && fs.existsSync(binaryPath) && that.isBinaryBusy(binaryPath)) {
226+
console.log('Binary is in use, waiting before retrying.');
227+
return setTimeout(function() { attemptAsync(waitsLeft - 1); }, that.BUSY_WAIT_MS);
228+
}
229+
fs.unlink(binaryPath, function() {
230+
that.download(conf, destParentDir, callback, retries - 1);
231+
});
208232
});
209233
};
210234
attemptAsync(that.BUSY_MAX_WAITS);

‎test/local_binary_busy_download.js‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,47 @@ describe('LocalBinary busy-binary download handling', function () {
130130
});
131131
});
132132

133+
// SDK-7713: parallel runs share ~/.browserstack/BrowserStackLocal.exe, and
134+
// while one run executes it the file is locked. A binary that still runs
135+
// must be reused, not replaced. Stubs are shell scripts, so skipped on Windows.
136+
(process.platform === 'win32' ? describe.skip : describe)('usable binary reuse', function () {
137+
var dir;
138+
beforeEach(function () { dir = fs.mkdtempSync(path.join(os.tmpdir(), 'bs-local-')); });
139+
140+
function stub(output) {
141+
var p = path.join(dir, 'BrowserStackLocal');
142+
fs.writeFileSync(p, '#!/bin/sh\necho "' + output + '"\n', { mode: 0o755 });
143+
return p;
144+
}
145+
146+
it('reuses a usable binary instead of re-downloading (sync)', function () {
147+
var binary = new LocalBinary(), downloaded = false, p = stub('BrowserStack Local version 8.9.36');
148+
binary.downloadSync = function () { downloaded = true; };
149+
150+
expect(binary.retryBinaryDownload({}, dir, null, 9, p)).to.equal(p);
151+
expect(downloaded).to.be(false);
152+
expect(fs.existsSync(p)).to.be(true);
153+
});
154+
155+
it('still replaces a binary that does not run (sync)', function () {
156+
var binary = new LocalBinary(), downloaded = false, p = stub('garbage');
157+
binary.downloadSync = function () { downloaded = true; };
158+
159+
binary.retryBinaryDownload({}, dir, null, 9, p);
160+
expect(downloaded).to.be(true);
161+
});
162+
163+
it('reuses a usable binary instead of re-downloading (async)', function (done) {
164+
var binary = new LocalBinary(), p = stub('BrowserStack Local version 8.9.36');
165+
binary.download = function () { done(new Error('should not re-download')); };
166+
167+
binary.retryBinaryDownload({}, dir, function (binaryPath) {
168+
expect(binaryPath).to.equal(p);
169+
done();
170+
}, 9, p);
171+
});
172+
});
173+
133174
describe('isBinaryBusy', function () {
134175
it('reports a readable file as free', function () {
135176
var binary = new LocalBinary(),

0 commit comments

Comments
 (0)