Skip to content

Commit 114e088

Browse files
gabrielschulhofRafaelGSS
authored andcommitted
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 <gabrielschulhof@gmail.com> PR-URL: #48933 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: theanarkh <theratliter@gmail.com>
1 parent d4398d4 commit 114e088

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/diagnostics_channel.js

+1-1
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);
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)