From 756ac6521875c2587a3b027fee88f30ebd584fb5 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 7 Jul 2020 23:30:04 +0200 Subject: [PATCH] http: fix crash for sync write errors during header parsing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/pull/34251 Fixes: https://github.com/nodejs/node/issues/15102 Fixes: https://github.com/nodejs/node/issues/34016 Reviewed-By: Fedor Indutny Reviewed-By: Gerhard Stöbich --- lib/_http_client.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index 9cf59c60577cc5..8f18b8181eb6cc 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -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