Skip to content

Commit

Permalink
http: fix crash for sync write errors during header parsing
Browse files Browse the repository at this point in the history
Fix a crash that occurs when `parser.finish()` is called during
`parser.execute()`. In this particular case, this happened because
a 100 continue response is a place in which `.end()` can be called
which can in turn lead to a write error, which is emitted
synchronously, thus inside the outer `parser.execute()` call.

Resolve that by delaying the `parser.finish()` call until after
the `parser.execute()` call is done.

This only affects v12.x, because on later versions, errors are not
emitted synchronously.

PR-URL: #34251
Fixes: #15102
Fixes: #34016
Reviewed-By: Fedor Indutny <[email protected]>
Reviewed-By: Gerhard Stöbich <[email protected]>
  • Loading branch information
addaleax committed Sep 22, 2020
1 parent 65b7bf4 commit 756ac65
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,12 @@ function socketErrorListener(err) {

const parser = socket.parser;
if (parser) {
parser.finish();
freeParser(parser, req, socket);
// Use process.nextTick() on v12.x since 'error' events might be
// emitted synchronously from e.g. a failed write() call on the socket.
process.nextTick(() => {
parser.finish();
freeParser(parser, req, socket);
});
}

// Ensure that no further data will come out of the socket
Expand Down

0 comments on commit 756ac65

Please sign in to comment.