Skip to content

Commit 0a426a3

Browse files
pranay-v29claude
andcommitted
Complete the async callback contract on every download path
Addresses the two findings on PR #185. node emits 'close' after 'error' on a write stream, so a failed attempt reported success through the close handler at the same time as starting a retry -- calling the caller back twice, once with a path that was never written. The retryOnce guard covered duplicate retries but not this. Skip completion once a retry has been triggered. Not 'finish', which can precede an open error when no data was written. retryBinaryDownload returned without calling the callback when retries were exhausted. That hole predates this branch, but it was previously unreachable on the async path: an early open failure crashed the child before exhaustion was possible. Now that the error is handled and retried, exhaustion is reachable, and Local.start() waits on a callback that never arrives -- trading a crash for a hang, which is worse for a test runner. The callback now completes with an empty path, and Local.start reports it the way startSync already does. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent eecad0c commit 0a426a3

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

‎lib/Local.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ function Local(){
105105
return callback();
106106

107107
this.getBinaryPath(function(binaryPath){
108+
/* Matches startSync's check below: the download can exhaust its retries
109+
and hand back nothing, and execFile(undefined) throws uncatchably. */
110+
if(!binaryPath) {
111+
return callback(new LocalError('Couldn\'t find binary file'));
112+
}
108113
that.binaryPath = binaryPath;
109114
try {
110115
fs.writeFileSync(that.logfile, '');

‎lib/LocalBinary.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ function LocalBinary(){
182182
var that = this;
183183
if(retries <= 0) {
184184
console.error('Number of retries to download exceeded.');
185+
/* The async contract has to be completed or Local.start() waits forever.
186+
An empty path is the signal; the caller reports it. */
187+
if(callback) callback();
185188
return;
186189
}
187190
console.log('Retrying Download. Retries left', retries);
@@ -329,6 +332,9 @@ function LocalBinary(){
329332
retryOnce('Got Error in binary download response', err);
330333
});
331334
fileStream.on('close', function () {
335+
/* node emits 'close' after 'error' too, so without this a failed
336+
attempt reports success alongside the retry it just started. */
337+
if(retried) return;
332338
fs.chmod(binaryPath, '0755', function() {
333339
callback(binaryPath);
334340
});

‎test/local_binary_busy_download.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,41 @@ describe('LocalBinary busy-binary download handling', function () {
5858
});
5959
});
6060

61+
describe('async download completion', function () {
62+
// The callback contract has to be completed on every path, or
63+
// Local.start() waits on a callback that never arrives.
64+
it('completes the callback when retries are exhausted', function (done) {
65+
var binary = new LocalBinary();
66+
binary.retryBinaryDownload({}, os.tmpdir(), function (binaryPath) {
67+
expect(binaryPath).to.be(undefined);
68+
done();
69+
}, 0, path.join(os.tmpdir(), 'bs-local-absent'));
70+
});
71+
72+
// node emits 'close' after 'error', so a failed attempt used to report
73+
// success through the close handler as well as retrying.
74+
it('reports a failed attempt once, not alongside a success', function (done) {
75+
var dir = fs.mkdtempSync(path.join(os.tmpdir(), 'bs-local-')),
76+
target = path.join(dir, 'BrowserStackLocal'),
77+
calls = [];
78+
fs.mkdirSync(target);
79+
80+
var binary = new LocalBinary();
81+
binary.getDownloadPath = function (conf, retries, cb) {
82+
cb(null, 'https://127.0.0.1:1/BrowserStackLocal');
83+
};
84+
binary.download({}, dir, function (binaryPath) { calls.push(binaryPath); }, 0);
85+
86+
setTimeout(function () {
87+
expect(calls.length).to.equal(1);
88+
expect(calls[0]).to.be(undefined);
89+
fs.rmdirSync(target);
90+
fs.rmdirSync(dir);
91+
done();
92+
}, 1500);
93+
});
94+
});
95+
6196
describe('isBinaryBusy', function () {
6297
it('reports a readable file as free', function () {
6398
var binary = new LocalBinary(),

0 commit comments

Comments
 (0)