Skip to content

Commit 6a56c68

Browse files
RaisinTenaduh95
authored andcommitted
http2: add diagnostics channel 'http2.server.stream.error'
Signed-off-by: Darshan Sen <[email protected]> PR-URL: #58512 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 93900b0 commit 6a56c68

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed

doc/api/diagnostics_channel.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,13 @@ Emitted when a stream is created on the server.
12521252

12531253
Emitted when a stream is started on the server.
12541254

1255+
`http2.server.stream.error`
1256+
1257+
* `stream` {ServerHttp2Stream}
1258+
* `error` {Error}
1259+
1260+
Emitted when an error occurs during the processing of a stream on the server.
1261+
12551262
#### Modules
12561263

12571264
> Stability: 1 - Experimental

lib/internal/http2/core.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ const onClientStreamFinishChannel = dc.channel('http2.client.stream.finish');
197197
const onClientStreamCloseChannel = dc.channel('http2.client.stream.close');
198198
const onServerStreamCreatedChannel = dc.channel('http2.server.stream.created');
199199
const onServerStreamStartChannel = dc.channel('http2.server.stream.start');
200+
const onServerStreamErrorChannel = dc.channel('http2.server.stream.error');
200201

201202
let debug = require('internal/util/debuglog').debuglog('http2', (fn) => {
202203
debug = fn;
@@ -2455,13 +2456,20 @@ class Http2Stream extends Duplex {
24552456
setImmediate(() => {
24562457
session[kMaybeDestroy]();
24572458
});
2458-
if (err &&
2459-
session[kType] === NGHTTP2_SESSION_CLIENT &&
2460-
onClientStreamErrorChannel.hasSubscribers) {
2461-
onClientStreamErrorChannel.publish({
2462-
stream: this,
2463-
error: err,
2464-
});
2459+
if (err) {
2460+
if (session[kType] === NGHTTP2_SESSION_CLIENT) {
2461+
if (onClientStreamErrorChannel.hasSubscribers) {
2462+
onClientStreamErrorChannel.publish({
2463+
stream: this,
2464+
error: err,
2465+
});
2466+
}
2467+
} else if (onServerStreamErrorChannel.hasSubscribers) {
2468+
onServerStreamErrorChannel.publish({
2469+
stream: this,
2470+
error: err,
2471+
});
2472+
}
24652473
}
24662474
callback(err);
24672475
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
// This test ensures that the built-in HTTP/2 diagnostics channels are reporting
8+
// the diagnostics messages for the 'http2.server.stream.error' channel when
9+
// an error occurs during the processing of a ServerHttp2Stream.
10+
11+
const assert = require('assert');
12+
const dc = require('diagnostics_channel');
13+
const http2 = require('http2');
14+
const { Duplex } = require('stream');
15+
16+
let expectedError = null;
17+
18+
dc.subscribe('http2.server.stream.error', common.mustCall(({ stream, error }) => {
19+
// Since ServerHttp2Stream is not exported from any module, this just checks
20+
// if the stream is an instance of Duplex and the constructor name is
21+
// 'ServerHttp2Stream'.
22+
assert.ok(stream instanceof Duplex);
23+
assert.strictEqual(stream.constructor.name, 'ServerHttp2Stream');
24+
assert.strictEqual(stream.closed, true);
25+
assert.strictEqual(stream.destroyed, true);
26+
27+
assert.ok(error);
28+
assert.strictEqual(error, expectedError);
29+
}));
30+
31+
const server = http2.createServer();
32+
33+
server.on('stream', common.mustCall((stream) => {
34+
stream.on('error', common.mustCall((err) => {
35+
assert.strictEqual(err, expectedError);
36+
}));
37+
38+
expectedError = new Error('HTTP/2 server stream error');
39+
stream.destroy(expectedError);
40+
}));
41+
42+
server.listen(0, common.mustCall(() => {
43+
const port = server.address().port;
44+
const client = http2.connect(`http://localhost:${port}`);
45+
46+
const stream = client.request({});
47+
stream.on('error', common.mustCall((err) => {
48+
assert.strictEqual(err.code, 'ERR_HTTP2_STREAM_ERROR');
49+
50+
client.close();
51+
server.close();
52+
}));
53+
}));

0 commit comments

Comments
 (0)