Skip to content

Commit 7d6b3f2

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 14e7bd8 commit 7d6b3f2

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)