Skip to content

Commit 2b868e8

Browse files
RaisinTentargos
authored andcommitted
http2: add diagnostics channel 'http2.server.stream.error'
Signed-off-by: Darshan Sen <raisinten@gmail.com> PR-URL: #58512 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent d7a2458 commit 2b868e8

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
@@ -1253,6 +1253,13 @@ Emitted when a stream is created on the server.
12531253

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

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

12581265
> Stability: 1 - Experimental

lib/internal/http2/core.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ const onClientStreamFinishChannel = dc.channel('http2.client.stream.finish');
192192
const onClientStreamCloseChannel = dc.channel('http2.client.stream.close');
193193
const onServerStreamCreatedChannel = dc.channel('http2.server.stream.created');
194194
const onServerStreamStartChannel = dc.channel('http2.server.stream.start');
195+
const onServerStreamErrorChannel = dc.channel('http2.server.stream.error');
195196

196197
let debug = require('internal/util/debuglog').debuglog('http2', (fn) => {
197198
debug = fn;
@@ -2453,13 +2454,20 @@ class Http2Stream extends Duplex {
24532454
setImmediate(() => {
24542455
session[kMaybeDestroy]();
24552456
});
2456-
if (err &&
2457-
session[kType] === NGHTTP2_SESSION_CLIENT &&
2458-
onClientStreamErrorChannel.hasSubscribers) {
2459-
onClientStreamErrorChannel.publish({
2460-
stream: this,
2461-
error: err,
2462-
});
2457+
if (err) {
2458+
if (session[kType] === NGHTTP2_SESSION_CLIENT) {
2459+
if (onClientStreamErrorChannel.hasSubscribers) {
2460+
onClientStreamErrorChannel.publish({
2461+
stream: this,
2462+
error: err,
2463+
});
2464+
}
2465+
} else if (onServerStreamErrorChannel.hasSubscribers) {
2466+
onServerStreamErrorChannel.publish({
2467+
stream: this,
2468+
error: err,
2469+
});
2470+
}
24632471
}
24642472
callback(err);
24652473
}
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)