Skip to content

Commit 8be7991

Browse files
committed
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: https://github.com/nodejs/node/issues/15385m
1 parent c1fce1e commit 8be7991

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)