Skip to content

Commit 670f845

Browse files
ryanseysstephenplusplus
authored andcommitted
storage: close sockets on error
1 parent b157b41 commit 670f845

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

lib/storage/file.js

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ File.prototype.createReadStream = function(options) {
401401
util.is(options.start, 'number') || util.is(options.end, 'number');
402402
var throughStream = streamEvents(through());
403403

404+
var requestStream;
405+
404406
var validations = ['crc32c', 'md5'];
405407
var validation;
406408

@@ -430,6 +432,22 @@ File.prototype.createReadStream = function(options) {
430432
o: encodeURIComponent(this.name)
431433
});
432434

435+
// End the stream, first emitting an error or complete event.
436+
var endThroughStream = once(function(err, resp) {
437+
if (err) {
438+
throughStream.emit('error', err, resp);
439+
} else {
440+
throughStream.emit('complete', resp);
441+
}
442+
443+
throughStream.destroy();
444+
});
445+
446+
var endRequestStream = once(function() {
447+
requestStream.abort();
448+
requestStream.destroy();
449+
});
450+
433451
createAuthorizedReq(remoteFilePath);
434452

435453
return throughStream;
@@ -458,7 +476,7 @@ File.prototype.createReadStream = function(options) {
458476
that.bucket.storage.makeAuthorizedRequest_(reqOpts, {
459477
onAuthorized: function(err, authorizedReqOpts) {
460478
if (err) {
461-
done(err, null);
479+
endThroughStream(err, null);
462480
return;
463481
}
464482

@@ -467,8 +485,13 @@ File.prototype.createReadStream = function(options) {
467485
var localCrcHash;
468486
var localMd5Hash = crypto.createHash('md5');
469487

470-
request(authorizedReqOpts)
471-
.on('error', done)
488+
requestStream = request(authorizedReqOpts);
489+
490+
requestStream
491+
.on('error', function(err) {
492+
endRequestStream();
493+
endThroughStream(err);
494+
})
472495

473496
.on('response', throughStream.emit.bind(throughStream, 'response'))
474497

@@ -485,13 +508,13 @@ File.prototype.createReadStream = function(options) {
485508
.on('complete', function(res) {
486509
util.handleResp(null, res, res.body, function(err, resp) {
487510
if (err) {
488-
done(err, resp);
511+
endThroughStream(err, resp);
489512
return;
490513
}
491514

492515
if (rangeRequest) {
493516
// Range requests can't receive data integrity checks.
494-
done(null, resp);
517+
endThroughStream(null, resp);
495518
return;
496519
}
497520

@@ -531,28 +554,19 @@ File.prototype.createReadStream = function(options) {
531554
].join(' '));
532555
mismatchError.code = 'CONTENT_DOWNLOAD_MISMATCH';
533556

534-
done(mismatchError, resp);
557+
endThroughStream(mismatchError, resp);
535558
} else {
536-
done(null, resp);
559+
endThroughStream(null, resp);
537560
}
538561
});
539562
})
540563

541-
.pipe(throughStream);
564+
.pipe(throughStream)
565+
566+
.on('error', endRequestStream);
542567
}
543568
});
544569
}
545-
546-
// End the stream, first emitting an error or complete event.
547-
function done(err) {
548-
if (err) {
549-
throughStream.emit('error', err);
550-
} else {
551-
throughStream.emit('complete');
552-
}
553-
554-
throughStream.end();
555-
}
556570
};
557571

558572
/**

test/storage/file.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ describe('File', function() {
385385
this.push(null);
386386
};
387387

388+
this.abort = util.noop;
389+
this.destroy = util.noop;
390+
388391
setImmediate(function() {
389392
that.emit('response', fakeResponse);
390393
that.emit('complete', fakeResponse);

0 commit comments

Comments
 (0)