Skip to content
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

nodejs/node#55116 #15

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,11 @@ module.exports.hasSyncUnsubscribeBug = hasSyncUnsubscribeBug;
function hasTracingChannelHasSubscribers() {
return MAJOR >= 22
|| (MAJOR == 20 && MINOR >= 13);
};
}
module.exports.hasTracingChannelHasSubscribers = hasTracingChannelHasSubscribers;

function hasSubscribersMutationBug() {
// TODO: version TBD
return MAJOR <= 22 && MINOR <= 9;
}
module.exports.hasSubscribersMutationBug = hasSubscribersMutationBug;
4 changes: 4 additions & 0 deletions dc-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@ if (!checks.hasTracingChannelHasSubscribers()) {
dc = require('./patch-tracing-channel-has-subscribers.js')(dc);
}

if (!checks.hasSubscribersMutationBug()) {
dc = require('./patch-subscribers-mutation-bug.js')(dc);
}

module.exports = dc;

45 changes: 45 additions & 0 deletions patch-subscribers-mutation-bug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const {
ArrayPrototypePushApply,

Check failure on line 2 in patch-subscribers-mutation-bug.js

View workflow job for this annotation

GitHub Actions / Code Linter

'ArrayPrototypePushApply' is assigned a value but never used
ArrayPrototypeSlice
} = require('./primordials.js');

// The ch.unsubscribe() method doesn't return a value
// Recent versions return if an unsubscribe succeeded
// @see https://github.com/nodejs/node/pull/40433
module.exports = function (unpatched) {
const channels = new WeakSet();

const dc_channel = unpatched.channel;

const dc = { ...unpatched };

dc.channel = function () {
const ch = dc_channel.apply(this, arguments);

if (channels.has(ch)) return ch;

const { subscribe, unsubscribe, publish } = ch;

Check failure on line 21 in patch-subscribers-mutation-bug.js

View workflow job for this annotation

GitHub Actions / Code Linter

'unsubscribe' is assigned a value but never used

ch.subscribe = function () {
this._subscribers = ArrayPrototypeSlice(this._subscribers);

return subscribe.apply(this, arguments);
};

ch.unsubscribe = function () {
//
};

ch.publish = function () {
const self = Object.assign({}, this);

return publish.apply(self, arguments)

Check failure on line 36 in patch-subscribers-mutation-bug.js

View workflow job for this annotation

GitHub Actions / Code Linter

Missing semicolon
};

channels.add(ch);

return ch;
};

return dc;
};
6 changes: 6 additions & 0 deletions primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const ReflectApply = Reflect.apply;
const PromiseReject = Promise.reject.bind(Promise);
const PromiseResolve = Promise.resolve;
const PromisePrototypeThen = makeCall(Promise.prototype.then);
const ArrayPrototypePush = makeCall(Array.prototype.push);
const ArrayPrototypePushApply = (...args) => Array.prototype.push.apply(...args);
const ArrayPrototypeSlice = makeCall(Array.prototype.slice);
const ArrayPrototypeSplice = makeCall(Array.prototype.splice);
const ArrayPrototypeAt = makeCall(Array.prototype.at || arrayAtPolyfill);
const ObjectDefineProperty = Object.defineProperty;
Expand All @@ -31,6 +34,9 @@ module.exports = {
PromiseReject,
PromiseResolve,
PromisePrototypeThen,
ArrayPrototypePush,
ArrayPrototypePushApply,
ArrayPrototypeSlice,
ArrayPrototypeSplice,
ArrayPrototypeAt,
ObjectDefineProperty,
Expand Down
1 change: 1 addition & 0 deletions test/test-diagnostics-channel-sync-unsubscribe.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ test('test-diagnostics-channel-sync-unsubscribe', (t) => {
const onMessageHandler = common.mustCall(() => dc.unsubscribe(channel_name, onMessageHandler));

dc.subscribe(channel_name, onMessageHandler);
dc.subscribe(channel_name, common.mustCall());

// This must not throw.
dc.channel(channel_name).publish(published_data);
Expand Down
Loading