Skip to content

Commit 798504a

Browse files
mcollinaBridgeAR
authored andcommitted
http2: make compat writeHead not crash if the stream is destroyed
Fixes: #24470 PR-URL: #24723 Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 3fe3bc9 commit 798504a

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/internal/http2/compat.js

+5
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,11 @@ class Http2ServerResponse extends Stream {
568568
if (this[kStream].headersSent)
569569
throw new ERR_HTTP2_HEADERS_SENT();
570570

571+
// If the stream is destroyed, we return false,
572+
// like require('http').
573+
if (this.stream.destroyed)
574+
return false;
575+
571576
if (typeof statusMessage === 'string')
572577
statusMessageWarn();
573578

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
const http2 = require('http2');
7+
8+
// Check that writeHead, write and end do not crash in compatibility mode
9+
10+
const server = http2.createServer(common.mustCall((req, res) => {
11+
// destroy the stream first
12+
req.stream.destroy();
13+
14+
res.writeHead(200);
15+
res.write('hello ');
16+
res.end('world');
17+
}));
18+
19+
server.listen(0, common.mustCall(() => {
20+
const client = http2.connect(`http://localhost:${server.address().port}`);
21+
22+
const req = client.request();
23+
req.on('response', common.mustNotCall());
24+
req.on('close', common.mustCall((arg) => {
25+
client.close();
26+
server.close();
27+
}));
28+
req.resume();
29+
}));

0 commit comments

Comments
 (0)