Skip to content

Commit

Permalink
Unpipe lengthAccumulator to prevent 'write after end' error (#1549)
Browse files Browse the repository at this point in the history
* lengthAccumulator responsible for 'write after end' error

* Added a unit test for connection timeout
  • Loading branch information
mrtcode authored and jeskew committed Jul 6, 2017
1 parent 17b15f4 commit e58d798
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ AWS.Request = inherit({
lengthAccumulator.on('end', checkContentLengthAndEmit);
stream.on('error', function(err) {
shouldCheckContentLength = false;
httpStream.unpipe(lengthAccumulator);
lengthAccumulator.emit('end');
lengthAccumulator.end();
});
Expand Down
19 changes: 19 additions & 0 deletions test/request.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,25 @@ describe('AWS.Request', function() {

// s.resume();
});

it('successfully handles connection timeout', function (done) {
var request, s;
app = function (req, resp) {
resp.writeHead(200, {
'content-length': 512 * 1024
});
resp.write(new Buffer(512 * 1024));
resp.end();
};
service.config.httpOptions.timeout = 5;
request = service.makeRequest('mockMethod');
s = request.createReadStream();
s.on('error', function (e) {
setImmediate(function () {
done();
});
});
});
});
}

Expand Down

0 comments on commit e58d798

Please sign in to comment.