-
-
Notifications
You must be signed in to change notification settings - Fork 36.1k
diagnostics_channel: grow native channel storage #64497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,7 +31,6 @@ const { | |
| const { triggerUncaughtException } = internalBinding('errors'); | ||
|
|
||
| const dc_binding = internalBinding('diagnostics_channel'); | ||
| const { subscribers: subscriberCounts } = dc_binding; | ||
|
|
||
| const { WeakReference, kEmptyObject } = require('internal/util'); | ||
| const { isPromise } = require('internal/util/types'); | ||
|
|
@@ -132,7 +131,7 @@ class ActiveChannel { | |
| this._subscribers = ArrayPrototypeSlice(this._subscribers); | ||
| ArrayPrototypePush(this._subscribers, subscription); | ||
| channels.incRef(this.name); | ||
| if (this._index !== undefined) subscriberCounts[this._index]++; | ||
| if (this._index !== undefined) dc_binding.subscribers[this._index]++; | ||
| } | ||
|
|
||
| unsubscribe(subscription) { | ||
|
|
@@ -145,7 +144,7 @@ class ActiveChannel { | |
| ArrayPrototypePushApply(this._subscribers, after); | ||
|
|
||
| channels.decRef(this.name); | ||
| if (this._index !== undefined) subscriberCounts[this._index]--; | ||
| if (this._index !== undefined) dc_binding.subscribers[this._index]--; | ||
| maybeMarkInactive(this); | ||
|
|
||
| return true; | ||
|
|
@@ -155,7 +154,7 @@ class ActiveChannel { | |
| const replacing = this._stores.has(store); | ||
| if (!replacing) { | ||
| channels.incRef(this.name); | ||
| if (this._index !== undefined) subscriberCounts[this._index]++; | ||
| if (this._index !== undefined) dc_binding.subscribers[this._index]++; | ||
| } | ||
| this._stores.set(store, transform); | ||
| } | ||
|
|
@@ -168,7 +167,7 @@ class ActiveChannel { | |
| this._stores.delete(store); | ||
|
|
||
| channels.decRef(this.name); | ||
| if (this._index !== undefined) subscriberCounts[this._index]--; | ||
| if (this._index !== undefined) dc_binding.subscribers[this._index]--; | ||
| maybeMarkInactive(this); | ||
|
|
||
| return true; | ||
|
|
@@ -209,7 +208,7 @@ class Channel { | |
| this._stores = undefined; | ||
| this.name = name; | ||
| if (typeof name === 'string') { | ||
| this._index = dc_binding.getOrCreateChannelIndex(name); | ||
| this._index = undefined; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this only done in case
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Symbol channels aren't supported/used on the native side, so we skip them here.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but if I get this right we either set explict |
||
| } | ||
|
|
||
| channels.set(name, this); | ||
|
|
@@ -640,7 +639,14 @@ function tracingChannel(nameOrChannels) { | |
| return new TracingChannel(nameOrChannels); | ||
| } | ||
|
|
||
| dc_binding.linkNativeChannel((name) => channel(name)); | ||
| dc_binding.linkNativeChannel((name, index) => { | ||
| const linkedChannel = channel(name); | ||
| linkedChannel._index = index; | ||
| dc_binding.subscribers[index] = | ||
| (linkedChannel._subscribers?.length || 0) + | ||
| (linkedChannel._stores?.size || 0); | ||
| return linkedChannel; | ||
| }); | ||
|
|
||
| module.exports = { | ||
| channel, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -669,7 +669,14 @@ function setupDiagnosticsChannel() { | |
| // JS references are cleared during serialization. | ||
| const dc = require('diagnostics_channel'); | ||
| const dc_binding = internalBinding('diagnostics_channel'); | ||
| dc_binding.linkNativeChannel((name) => dc.channel(name)); | ||
| dc_binding.linkNativeChannel((name, index) => { | ||
| const channel = dc.channel(name); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess there is no simple way to avoid code duplication with lib/diagnostics_channel.js right? |
||
| channel._index = index; | ||
| dc_binding.subscribers[index] = | ||
| (channel._subscribers?.length || 0) + | ||
| (channel._stores?.size || 0); | ||
| return channel; | ||
| }); | ||
| } | ||
|
|
||
| function initializePermission() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('node:assert'); | ||
| const dc = require('node:diagnostics_channel'); | ||
|
|
||
| let last; | ||
| for (let i = 0; i < 1024 * 10 + 1; i++) { | ||
| last = dc.channel(`test:many-channels:${i}`); | ||
| } | ||
|
|
||
| const onMessage = common.mustCall((message, name) => { | ||
| assert.strictEqual(message, 'message'); | ||
| assert.strictEqual(name, last.name); | ||
| }); | ||
|
|
||
| last.subscribe(onMessage); | ||
| last.publish('message'); | ||
| assert.strictEqual(last.unsubscribe(onMessage), true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand this right the growing of
subscribersdisallows destruction here as we would end up in a dangling reference.Do we have any tools in hand to detect such subtle import issues?