Skip to content

Commit 0ee6715

Browse files
simon-idruyadorno
authored andcommitted
diagnostics_channel: fix unsubscribe during publish
PR-URL: #55116 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Claudio Wunder <cwunder@gnome.org>
1 parent 4f84cdc commit 0ee6715

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

‎lib/diagnostics_channel.js‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const {
44
ArrayPrototypeAt,
55
ArrayPrototypeIndexOf,
66
ArrayPrototypePush,
7+
ArrayPrototypePushApply,
8+
ArrayPrototypeSlice,
79
ArrayPrototypeSplice,
810
ObjectDefineProperty,
911
ObjectGetPrototypeOf,
@@ -97,6 +99,7 @@ function wrapStoreRun(store, data, next, transform = defaultTransform) {
9799
classActiveChannel{
98100
subscribe(subscription){
99101
validateFunction(subscription,'subscription');
102+
this._subscribers=ArrayPrototypeSlice(this._subscribers);
100103
ArrayPrototypePush(this._subscribers,subscription);
101104
channels.incRef(this.name);
102105
}
@@ -105,7 +108,10 @@ class ActiveChannel {
105108
constindex=ArrayPrototypeIndexOf(this._subscribers,subscription);
106109
if(index===-1)returnfalse;
107110

108-
ArrayPrototypeSplice(this._subscribers,index,1);
111+
constbefore=ArrayPrototypeSlice(this._subscribers,0,index);
112+
constafter=ArrayPrototypeSlice(this._subscribers,index+1);
113+
this._subscribers=before;
114+
ArrayPrototypePushApply(this._subscribers,after);
109115

110116
channels.decRef(this.name);
111117
maybeMarkInactive(this);
@@ -137,9 +143,10 @@ class ActiveChannel {
137143
}
138144

139145
publish(data){
140-
for(leti=0;i<(this._subscribers?.length||0);i++){
146+
constsubscribers=this._subscribers;
147+
for(leti=0;i<(subscribers?.length||0);i++){
141148
try{
142-
constonMessage=this._subscribers[i];
149+
constonMessage=subscribers[i];
143150
onMessage(data,this.name);
144151
}catch(err){
145152
process.nextTick(()=>{

‎test/parallel/test-diagnostics-channel-sync-unsubscribe.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const published_data = 'some message';
99
constonMessageHandler=common.mustCall(()=>dc.unsubscribe(channel_name,onMessageHandler));
1010

1111
dc.subscribe(channel_name,onMessageHandler);
12+
dc.subscribe(channel_name,common.mustCall());
1213

1314
// This must not throw.
1415
dc.channel(channel_name).publish(published_data);

0 commit comments

Comments
 (0)