-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Since we were removing the hasRef() method before exposing the MessagePort object, the only way of knowing if the handle was keeping the event loop active was to parse the string returned by util.inspect(port), which is inconvenient and inconsistent with most of the other async resources. So this change stops removing hasRef() from the MessagePort prototype. The reason why this is also being documented is that while reporting active resources, async_hooks returns the same MessagePort object as the one that is accessible by users. Refs: #42091 (comment) Signed-off-by: Darshan Sen <raisinten@gmail.com> PR-URL: #42849 Reviewed-By: Anna Henningsen <anna@addaleax.net>
- Loading branch information
Showing
5 changed files
with
120 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,57 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
|
||
const { MessageChannel } = require('worker_threads'); | ||
const { createHook } = require('async_hooks'); | ||
const { strictEqual } = require('assert'); | ||
|
||
const handles = []; | ||
|
||
createHook({ | ||
init(asyncId, type, triggerAsyncId, resource) { | ||
if (type === 'MESSAGEPORT') { | ||
handles.push(resource); | ||
} | ||
} | ||
}).enable(); | ||
|
||
const { port1, port2 } = new MessageChannel(); | ||
strictEqual(handles[0], port1); | ||
strictEqual(handles[1], port2); | ||
|
||
strictEqual(handles[0].hasRef(), false); | ||
strictEqual(handles[1].hasRef(), false); | ||
|
||
port1.unref(); | ||
strictEqual(handles[0].hasRef(), false); | ||
|
||
port1.ref(); | ||
strictEqual(handles[0].hasRef(), true); | ||
|
||
port1.unref(); | ||
strictEqual(handles[0].hasRef(), false); | ||
|
||
port1.on('message', () => {}); | ||
strictEqual(handles[0].hasRef(), true); | ||
|
||
port2.unref(); | ||
strictEqual(handles[1].hasRef(), false); | ||
|
||
port2.ref(); | ||
strictEqual(handles[1].hasRef(), true); | ||
|
||
port2.unref(); | ||
strictEqual(handles[1].hasRef(), false); | ||
|
||
port2.on('message', () => {}); | ||
strictEqual(handles[0].hasRef(), true); | ||
|
||
port1.on('close', common.mustCall(() => { | ||
strictEqual(handles[0].hasRef(), false); | ||
strictEqual(handles[1].hasRef(), false); | ||
})); | ||
|
||
port2.close(); | ||
|
||
strictEqual(handles[0].hasRef(), true); | ||
strictEqual(handles[1].hasRef(), true); |
This file contains 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
This file contains 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,45 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
|
||
const { Worker } = require('worker_threads'); | ||
const { createHook } = require('async_hooks'); | ||
const { deepStrictEqual, strictEqual } = require('assert'); | ||
|
||
const m = new Map(); | ||
createHook({ | ||
init(asyncId, type, triggerAsyncId, resource) { | ||
if (['WORKER', 'MESSAGEPORT'].includes(type)) { | ||
m.set(asyncId, { type, resource }); | ||
} | ||
}, | ||
destroy(asyncId) { | ||
m.delete(asyncId); | ||
} | ||
}).enable(); | ||
|
||
function getActiveWorkerAndMessagePortTypes() { | ||
const activeWorkerAndMessagePortTypes = []; | ||
for (const asyncId of m.keys()) { | ||
const { type, resource } = m.get(asyncId); | ||
// Same logic as https://github.com/mafintosh/why-is-node-running/blob/24fb4c878753390a05d00959e6173d0d3c31fddd/index.js#L31-L32. | ||
if (typeof resource.hasRef !== 'function' || resource.hasRef() === true) { | ||
activeWorkerAndMessagePortTypes.push(type); | ||
} | ||
} | ||
return activeWorkerAndMessagePortTypes; | ||
} | ||
|
||
const w = new Worker('', { eval: true }); | ||
deepStrictEqual(getActiveWorkerAndMessagePortTypes(), ['WORKER']); | ||
w.unref(); | ||
deepStrictEqual(getActiveWorkerAndMessagePortTypes(), []); | ||
w.ref(); | ||
deepStrictEqual(getActiveWorkerAndMessagePortTypes(), ['WORKER', 'MESSAGEPORT']); | ||
|
||
w.on('exit', common.mustCall((exitCode) => { | ||
strictEqual(exitCode, 0); | ||
deepStrictEqual(getActiveWorkerAndMessagePortTypes(), ['WORKER']); | ||
setTimeout(common.mustCall(() => { | ||
deepStrictEqual(getActiveWorkerAndMessagePortTypes(), []); | ||
}), 0); | ||
})); |