From 62c449b682c85409ae774cd3f9ce40c89a6029a3 Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Tue, 16 Jun 2020 23:22:15 +0530 Subject: [PATCH 1/2] http2: always call callback on Http2ServerResponse#end Fixes: https://github.com/nodejs/node/issues/28001 --- lib/internal/http2/compat.js | 13 ++++++++----- .../test-http2-compat-serverresponse-end.js | 12 ++++++------ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js index 841f9b69a033cd..19daf83de1f9f7 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -709,11 +709,6 @@ class Http2ServerResponse extends Stream { const stream = this[kStream]; const state = this[kState]; - if ((state.closed || state.ending) && - state.headRequest === stream.headRequest) { - return this; - } - if (typeof chunk === 'function') { cb = chunk; chunk = null; @@ -722,6 +717,14 @@ class Http2ServerResponse extends Stream { encoding = 'utf8'; } + if ((state.closed || state.ending) && + state.headRequest === stream.headRequest) { + if (cb) { + process.nextTick(cb); + } + return this; + } + if (chunk !== null && chunk !== undefined) this.write(chunk, encoding); diff --git a/test/parallel/test-http2-compat-serverresponse-end.js b/test/parallel/test-http2-compat-serverresponse-end.js index 8505d6c4969db1..52d4c603e95e04 100644 --- a/test/parallel/test-http2-compat-serverresponse-end.js +++ b/test/parallel/test-http2-compat-serverresponse-end.js @@ -25,16 +25,16 @@ const { // but callback will only be called once const server = createServer(mustCall((request, response) => { response.end('end', 'utf8', mustCall(() => { - response.end(mustNotCall()); + response.end(mustCall()); process.nextTick(() => { - response.end(mustNotCall()); + response.end(mustCall()); server.close(); }); })); response.on('finish', mustCall(() => { - response.end(mustNotCall()); + response.end(mustCall()); })); - response.end(mustNotCall()); + response.end(mustCall()); })); server.listen(0, mustCall(() => { let data = ''; @@ -294,7 +294,7 @@ const { })); response.end('data', mustCall(() => { strictEqual(finished, false); - response.end('data', mustNotCall()); + response.end('data', mustCall()); })); })); server.listen(0, mustCall(() => { @@ -328,7 +328,7 @@ const { // Should be able to respond to HEAD with just .end const server = createServer(mustCall((request, response) => { response.end('data', mustCall()); - response.end(mustNotCall()); + response.end(mustCall()); })); server.listen(0, mustCall(() => { const { port } = server.address(); From 14fa43f0e709f132c957db8a22d2d3be532e841f Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Wed, 17 Jun 2020 08:12:02 +0530 Subject: [PATCH 2/2] Update lib/internal/http2/compat.js Co-authored-by: Denys Otrishko --- lib/internal/http2/compat.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js index 19daf83de1f9f7..8b0f66549a3195 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -719,7 +719,7 @@ class Http2ServerResponse extends Stream { if ((state.closed || state.ending) && state.headRequest === stream.headRequest) { - if (cb) { + if (typeof cb === 'function') { process.nextTick(cb); } return this;