Skip to content
Closed
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
20 changes: 20 additions & 0 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@ MessagePort.prototype.close = function(cb) {
const drainMessagePort = MessagePort.prototype.drain;
delete MessagePort.prototype.drain;

Object.defineProperty(MessagePort.prototype, util.inspect.custom, {
enumerable: false,
writable: false,
value: function inspect() {
let ref;
try {
// This may throw when `this` does not refer to a native object,
// e.g. when accessing the prototype directly.
ref = this.hasRef();
} catch { return this; }
return Object.assign(Object.create(MessagePort.prototype),
ref === undefined ? {
active: false,
} : {
active: true,
refed: ref
}, this);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: could you please put this on a separate line as it's kind of confusing?
Also, I'd suggest extracting second argument creation to a separate variable to make it easier to understand =). Though I'm fine with this too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (moving to a separate line)

}
});

function setupPortReferencing(port, eventEmitter, eventName) {
// Keep track of whether there are any workerMessage listeners:
// If there are some, ref() the channel so it keeps the event loop alive.
Expand Down
18 changes: 16 additions & 2 deletions test/parallel/test-worker-message-port-transfer-self.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const common = require('../common');
const assert = require('assert');
const util = require('util');
const { MessageChannel } = require('worker_threads');

const { port1, port2 } = new MessageChannel();
Expand All @@ -25,9 +26,22 @@ assert.throws(common.mustCall(() => {
// The failed transfer should not affect the ports in anyway.
port2.onmessage = common.mustCall((message) => {
assert.strictEqual(message, 2);

assert(util.inspect(port1).includes('active: true'), util.inspect(port1));
assert(util.inspect(port2).includes('active: true'), util.inspect(port2));

port1.close();

setTimeout(common.mustNotCall('The communication channel is still open'),
common.platformTimeout(1000)).unref();
tick(10, () => {
assert(util.inspect(port1).includes('active: false'), util.inspect(port1));
assert(util.inspect(port2).includes('active: false'), util.inspect(port2));
});
});
port1.postMessage(2);

function tick(n, cb) {
if (--n > 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually makes half-the-n ticks, was this intentional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. Thanks for catching!

return setImmediate(() => tick(n-1, cb));
else
cb();
}