From c46543d2a0e44a19194b194c4836843b8a5e0e41 Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Tue, 16 Jun 2020 23:22:15 +0530 Subject: [PATCH] http2: always call callback on Http2ServerResponse#end Fixes: https://github.com/nodejs/node/issues/28001 PR-URL: https://github.com/nodejs/node/pull/33911 Reviewed-By: Anna Henningsen Reviewed-By: Denys Otrishko Reviewed-By: Luigi Pinca --- 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 a1c61284ccbc09..00f5ecfce24543 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -698,11 +698,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; @@ -711,6 +706,14 @@ class Http2ServerResponse extends Stream { encoding = 'utf8'; } + if ((state.closed || state.ending) && + state.headRequest === stream.headRequest) { + if (typeof cb === 'function') { + 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();