diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js index 37f8242b0068f3..516954587108a8 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -589,14 +589,11 @@ class Http2ServerResponse extends Stream { writeHead(statusCode, statusMessage, headers) { const state = this[kState]; - if (state.closed) - throw new ERR_HTTP2_INVALID_STREAM(); + if (state.closed || this.stream.destroyed) + return this; if (this[kStream].headersSent) throw new ERR_HTTP2_HEADERS_SENT(); - if (this.stream.destroyed) - return this; - if (typeof statusMessage === 'string') statusMessageWarn(); diff --git a/test/parallel/test-http2-compat-serverresponse-writehead.js b/test/parallel/test-http2-compat-serverresponse-writehead.js index aabcf18ad9e0cf..e66f5f79cd9041 100644 --- a/test/parallel/test-http2-compat-serverresponse-writehead.js +++ b/test/parallel/test-http2-compat-serverresponse-writehead.js @@ -26,9 +26,9 @@ server.listen(0, common.mustCall(function() { response.on('finish', common.mustCall(function() { server.close(); process.nextTick(common.mustCall(() => { - common.expectsError(() => { response.writeHead(300); }, { - code: 'ERR_HTTP2_INVALID_STREAM' - }); + // The stream is invalid at this point, + // and this line verifies this does not throw. + response.writeHead(300); })); })); response.end();