From 1d397423ef0c12becb9f6f0e8e23728081270053 Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Thu, 28 Feb 2019 17:45:09 +0100 Subject: [PATCH] http: only destroy socket once error is sent This patch fixes a race condition: sometimes the socket is destroyed before the data is sent. If node is proxied by, for example, an AWS ELB, a 504 error would be returned instead of the expected 400. --- lib/_http_server.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/_http_server.js b/lib/_http_server.js index 02cd7a81a9cc0b..36a0e2b006aab5 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -521,9 +521,12 @@ function socketOnError(e) { if (this.writable) { const response = e.code === 'HPE_HEADER_OVERFLOW' ? requestHeaderFieldsTooLargeResponse : badRequestResponse; - this.write(response); + this.write(response, () => { + this.destroy(e); + }); + } else { + this.destroy(e); } - this.destroy(e); } }