Skip to content

Commit b61220e

Browse files
apapirovskijasnell
authored andcommitted
http2: fix subsequent end calls to not throw
Calling Http2ServerResponse.end multiple times should never cause the code to throw an error, subsequent calls should instead return false. Fix behaviour to match http1. Fixes: #15385 PR-URL: #15414 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent ba96c8f commit b61220e

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

lib/internal/http2/compat.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,14 +466,13 @@ class Http2ServerResponse extends Stream {
466466
cb = encoding;
467467
encoding = 'utf8';
468468
}
469+
if (stream === undefined || stream.finished === true) {
470+
return false;
471+
}
469472
if (chunk !== null && chunk !== undefined) {
470473
this.write(chunk, encoding);
471474
}
472475

473-
if (stream === undefined) {
474-
return;
475-
}
476-
477476
if (typeof cb === 'function') {
478477
stream.once('finish', cb);
479478
}

test/parallel/test-http2-compat-serverresponse-end.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
const { mustCall, mustNotCall, hasCrypto, skip } = require('../common');
55
if (!hasCrypto)
66
skip('missing crypto');
7-
const { strictEqual } = require('assert');
7+
const { doesNotThrow, strictEqual } = require('assert');
88
const {
99
createServer,
1010
connect,
@@ -19,6 +19,9 @@ const {
1919
// but may be invoked repeatedly without throwing errors.
2020
const server = createServer(mustCall((request, response) => {
2121
strictEqual(response.closed, false);
22+
response.on('finish', mustCall(() => process.nextTick(
23+
mustCall(() => doesNotThrow(() => response.end('test', mustNotCall())))
24+
)));
2225
response.end(mustCall(() => {
2326
server.close();
2427
}));

0 commit comments

Comments
 (0)