Skip to content

Commit a4e923f

Browse files
apapirovskimcollina
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 bd85752 commit a4e923f

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
@@ -458,14 +458,13 @@ class Http2ServerResponse extends Stream {
458458
cb = encoding;
459459
encoding = 'utf8';
460460
}
461+
if (stream === undefined || stream.finished === true) {
462+
return false;
463+
}
461464
if (chunk !== null && chunk !== undefined) {
462465
this.write(chunk, encoding);
463466
}
464467

465-
if (stream === undefined) {
466-
return;
467-
}
468-
469468
if (typeof cb === 'function') {
470469
stream.once('finish', cb);
471470
}

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)