-
Notifications
You must be signed in to change notification settings - Fork 1
fix: memoize channel(name) to make Channel identity stable per name #27
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,16 @@ | ||
| // There's a bug where a newly created channel is immediately garbage collected | ||
| // @see https://github.com/nodejs/node/pull/47520 | ||
| // | ||
| // The phony subscriber below keeps a created channel alive, but Node's channel(name) | ||
| // can still return a brand-new Channel object for the same name on a subsequent call | ||
| // (observed on Node 18 under certain workloads). A WeakSet keyed by Channel-object | ||
| // identity can't merge those — each new object is treated as new and gets its own | ||
| // phony subscriber. Callers that captured the earlier Channel in a closure end up | ||
| // publishing to a different object than later subscribe()-ers attach to. | ||
| // | ||
| // To make channel identity stable per name (the contract callers expect), memoize | ||
| // dc.channel(name) by name and short-circuit before delegating to the underlying | ||
| // channel() on subsequent calls. | ||
| const PHONY_SUBSCRIBE = function AVOID_GARBAGE_COLLECTION() {}; | ||
|
|
||
| const { | ||
|
|
@@ -10,11 +21,15 @@ const { | |
| module.exports = function(unpatched) { | ||
| const dc_channel = unpatched.channel; | ||
| const channels = new WeakSet(); | ||
| const byName = new Map(); | ||
|
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. This will grow forever but I think that's acceptable as channels should not be high cardinality and are pretty lightweight. |
||
|
|
||
| const dc = { ...unpatched }; | ||
|
|
||
| dc.channel = function() { | ||
| const name = arguments[0]; | ||
| if (byName.has(name)) return byName.get(name); | ||
| const ch = dc_channel.apply(this, arguments); | ||
| byName.set(name, ch); | ||
|
|
||
| if (channels.has(ch)) return ch; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| const test = require('tape'); | ||
| const patch = require('../patch-garbage-collection-bug.js'); | ||
|
|
||
| // Simulate the Node 18 bug: the underlying channel(name) returns a | ||
| // brand-new Channel object on every call for the same name, even when | ||
| // the previous one is still held alive by JS code. The patch's job is | ||
| // to make dc.channel(name) return a stable Channel identity per name | ||
| // regardless of what the underlying registry returns. | ||
| function mockUnpatched() { | ||
| const calls = { count: 0 }; | ||
| function channel(name) { | ||
| calls.count++; | ||
| return { | ||
| _subscribers: [], | ||
| _stores: new Map(), | ||
| _name: name, | ||
| _instanceId: calls.count, | ||
| subscribe(fn) { this._subscribers.push(fn); }, | ||
| unsubscribe(fn) { | ||
| const i = this._subscribers.indexOf(fn); | ||
| if (i >= 0) this._subscribers.splice(i, 1); | ||
| return i >= 0; | ||
| }, | ||
| publish(data) { | ||
| for (const sub of this._subscribers) sub(data); | ||
| } | ||
| }; | ||
| } | ||
| return { channel, calls }; | ||
| } | ||
|
|
||
| test('garbage-collection patch: dc.channel(name) returns stable identity across calls', t => { | ||
| const { channel, calls } = mockUnpatched(); | ||
| const dc = patch({ channel }); | ||
|
|
||
| const a = dc.channel('foo'); | ||
| const b = dc.channel('foo'); | ||
| const c = dc.channel('foo'); | ||
|
|
||
| t.strictEqual(a, b, 'second call to dc.channel(name) returns same Channel object'); | ||
| t.strictEqual(b, c, 'third call returns same Channel object'); | ||
| t.ok(calls.count >= 1, 'underlying channel() was called at least once for first lookup'); | ||
|
|
||
| const callsAfterMemoization = calls.count; | ||
| dc.channel('foo'); | ||
| dc.channel('foo'); | ||
| t.equal(calls.count, callsAfterMemoization, | ||
| 'memoized lookups do not re-invoke the underlying channel()'); | ||
|
|
||
| t.end(); | ||
| }); | ||
|
|
||
| test('garbage-collection patch: distinct names get distinct Channel objects', t => { | ||
| const { channel } = mockUnpatched(); | ||
| const dc = patch({ channel }); | ||
|
|
||
| const foo = dc.channel('foo'); | ||
| const bar = dc.channel('bar'); | ||
|
|
||
| t.notStrictEqual(foo, bar, 'different names return different Channel objects'); | ||
| t.strictEqual(dc.channel('foo'), foo, 'foo memoization holds'); | ||
| t.strictEqual(dc.channel('bar'), bar, 'bar memoization holds'); | ||
| t.end(); | ||
| }); | ||
|
|
||
| test('garbage-collection patch: subscribers attach to the memoized Channel and receive publishes', t => { | ||
| // This is the end-to-end shape the bug produces: the publisher captures | ||
| // a Channel from one call, the subscriber attaches via a later call. | ||
| // Without memoization (when the underlying channel() misbehaves) the | ||
| // two see different Channel objects and the subscriber never fires. | ||
| const { channel } = mockUnpatched(); | ||
| const dc = patch({ channel }); | ||
|
|
||
| const publisher = dc.channel('observable'); | ||
|
|
||
| let received = null; | ||
| dc.channel('observable').subscribe((data) => { received = data; }); | ||
|
|
||
| publisher.publish('hello'); | ||
|
|
||
| t.equal(received, 'hello', 'subscriber attached to a later dc.channel(name) lookup receives publishes from the earlier-captured Channel'); | ||
| t.end(); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.