Skip to content

Commit a66b391

Browse files
mcollinatargos
authored andcommitted
http2: do no throw in writeHead if state.closed
The http1 implementation does not throw if the connection is down. The http2 compat implementation should do the same. See: fastify/fastify-http-proxy#51. See: fastify/fastify#1494. PR-URL: #27682 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 05c3d53 commit a66b391

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

lib/internal/http2/compat.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -589,14 +589,11 @@ class Http2ServerResponse extends Stream {
589589
writeHead(statusCode, statusMessage, headers) {
590590
const state = this[kState];
591591

592-
if (state.closed)
593-
throw new ERR_HTTP2_INVALID_STREAM();
592+
if (state.closed || this.stream.destroyed)
593+
return this;
594594
if (this[kStream].headersSent)
595595
throw new ERR_HTTP2_HEADERS_SENT();
596596

597-
if (this.stream.destroyed)
598-
return this;
599-
600597
if (typeof statusMessage === 'string')
601598
statusMessageWarn();
602599

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ server.listen(0, common.mustCall(function() {
2626
response.on('finish', common.mustCall(function() {
2727
server.close();
2828
process.nextTick(common.mustCall(() => {
29-
common.expectsError(() => { response.writeHead(300); }, {
30-
code: 'ERR_HTTP2_INVALID_STREAM'
31-
});
29+
// The stream is invalid at this point,
30+
// and this line verifies this does not throw.
31+
response.writeHead(300);
3232
}));
3333
}));
3434
response.end();

0 commit comments

Comments
 (0)