Skip to content

Commit eb504c9

Browse files
diagnostics_channel: fix last subscriber removal
When iterating over diagnostics channel subscribers, assume their count is zero if the list of subscribers becomes undefined, because there may be only one subscriber which may unsubscribe itself as part of its onMessage handler. Signed-off-by: Gabriel Schulhof <[email protected]> PR-URL: #48933 Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: theanarkh <[email protected]>
1 parent 97ca9a8 commit eb504c9

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/diagnostics_channel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class ActiveChannel {
136136
}
137137

138138
publish(data) {
139-
for (let i = 0; i < this._subscribers.length; i++) {
139+
for (let i = 0; i < (this._subscribers?.length || 0); i++) {
140140
try {
141141
const onMessage = this._subscribers[i];
142142
onMessage(data, this.name);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const dc = require('node:diagnostics_channel');
5+
6+
const channel_name = 'test:channel';
7+
const published_data = 'some message';
8+
9+
const onMessageHandler = common.mustCall(() => dc.unsubscribe(channel_name, onMessageHandler));
10+
11+
dc.subscribe(channel_name, onMessageHandler);
12+
13+
// This must not throw.
14+
dc.channel(channel_name).publish(published_data);

0 commit comments

Comments
 (0)