Skip to content

Commit c1aa103

Browse files
diagnostics_channel: handle 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.
1 parent 8f0f17e commit c1aa103

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

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)