Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http2: make compat writeHead not crash if the stream is destroyed #24723

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,11 @@ class Http2ServerResponse extends Stream {
if (this[kStream].headersSent)
throw new ERR_HTTP2_HEADERS_SENT();

// If the stream is destroyed, we return false,
// like require('http').
if (this.stream.destroyed)
return false;

if (typeof statusMessage === 'string')
statusMessageWarn();

Expand Down
29 changes: 29 additions & 0 deletions test/parallel/test-http2-compat-write-head-destroyed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const http2 = require('http2');

// Check that writeHead, write and end do not crash in compatibility mode

const server = http2.createServer(common.mustCall((req, res) => {
// destroy the stream first
req.stream.destroy();

res.writeHead(200);
res.write('hello ');
res.end('world');
}));

server.listen(0, common.mustCall(() => {
const client = http2.connect(`http://localhost:${server.address().port}`);

const req = client.request();
req.on('response', common.mustNotCall());
req.on('close', common.mustCall((arg) => {
client.close();
server.close();
}));
req.resume();
}));