From bf2dc9e5d16600e19194ebc78a30b8f4dd4cadb1 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Sat, 15 Aug 2026 14:14:25 -0700 Subject: [PATCH] diagnostics_channel: validate before channel activation Validate the first subscription before calling markActive(). This prevents invalid callbacks from leaving channels active and notifying native consumers without an installed subscriber. Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> Assisted-by: codex:gpt-5.6-sol --- lib/diagnostics_channel.js | 1 + .../parallel/test-diagnostics-channel-object-channel-pub-sub.js | 2 ++ test/parallel/test-diagnostics-channel-pub-sub.js | 2 ++ 3 files changed, 5 insertions(+) diff --git a/lib/diagnostics_channel.js b/lib/diagnostics_channel.js index 998f17aa61d6..7c1f4fa845ed 100644 --- a/lib/diagnostics_channel.js +++ b/lib/diagnostics_channel.js @@ -229,6 +229,7 @@ class Channel { } subscribe(subscription) { + validateFunction(subscription, 'subscription'); markActive(this); this.subscribe(subscription); } diff --git a/test/parallel/test-diagnostics-channel-object-channel-pub-sub.js b/test/parallel/test-diagnostics-channel-object-channel-pub-sub.js index 9498419b806c..f02544936559 100644 --- a/test/parallel/test-diagnostics-channel-object-channel-pub-sub.js +++ b/test/parallel/test-diagnostics-channel-object-channel-pub-sub.js @@ -44,3 +44,5 @@ assert.ok(!channel.unsubscribe(subscriber)); assert.throws(() => { channel.subscribe(null); }, { code: 'ERR_INVALID_ARG_TYPE' }); +assert.ok(!channel.hasSubscribers); +assert.ok(!dc.hasSubscribers('test')); diff --git a/test/parallel/test-diagnostics-channel-pub-sub.js b/test/parallel/test-diagnostics-channel-pub-sub.js index a7232ab58ce8..e3a868b7ce0c 100644 --- a/test/parallel/test-diagnostics-channel-pub-sub.js +++ b/test/parallel/test-diagnostics-channel-pub-sub.js @@ -42,6 +42,8 @@ assert.ok(!dc.unsubscribe(name, subscriber)); assert.throws(() => { dc.subscribe(name, null); }, { code: 'ERR_INVALID_ARG_TYPE' }); +assert.ok(!channel.hasSubscribers); +assert.ok(!dc.hasSubscribers(name)); // Reaching zero subscribers should not delete from the channels map as there // will be no more weakref to incRef if another subscribe happens while the