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