-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Add hasRef
to worker_threads.Worker
#42091
Comments
What about other APIs that have an |
I haven't gotten a bug report in Jest about them, so I have no opinion 😀 On a more serious note, I think it makes sense for |
@SimenB would you be interested in contributing a PR? I am happy to guide you, the code is in |
Io.js, that's a blast from the past! Happy to contribute it if it'll get accepted 🙂 |
Refs: nodejs#42091 The issue above reported a specific use case about `Timer.hasRef`. After reading the issue, I started thinking users may naturally expect `hasRef` method if `ref` and `unref` exist on an object they use. As of now, however, `hasRef` exists on `Timer` only. This commit suggests adding `hasRef` method to `ChildProcess` first. There are more similar cases. (fs.FSWatcher, fs.StatWatcher, net.Socket, and so on)
Refs: nodejs#42091 The issue above reported a specific use case about `Timer.hasRef()`. After reading the issue, I started thinking users may naturally expect `hasRef` method if `ref()` and `unref()` exist on an object they use. As of now, however, `hasRef()` exists on `Timer` only. This commit suggests adding `hasRef` method to `ChildProcess` first. There are more similar cases. (fs.FSWatcher, fs.StatWatcher, net.Socket, and so on)
Refs: nodejs#42091 The issue above reported a specific use case about `hasRef()`. After reading the issue, I started thinking users may naturally expect `hasRef` method if `ref()` and `unref()` exist on an object they use. As of now, however, `hasRef()` exists on `Timer` only. This commit suggests adding `hasRef` method to `ChildProcess` first. There are more similar cases. (fs.FSWatcher, fs.StatWatcher, net.Socket, and so on)
This should help projects like https://github.com/mafintosh/why-is-node-running and https://github.com/facebook/jest to detect if Worker instances are keeping the event loop active correctly. Fixes: nodejs#42091 Refs: mafintosh/why-is-node-running#59 Signed-off-by: Darshan Sen <[email protected]>
Sent a PR for this: #42756 |
This should help projects like https://github.com/mafintosh/why-is-node-running and https://github.com/facebook/jest to detect if Worker instances are keeping the event loop active correctly. Fixes: nodejs#42091 Refs: mafintosh/why-is-node-running#59 Signed-off-by: Darshan Sen <[email protected]>
This should help projects like https://github.com/mafintosh/why-is-node-running and https://github.com/facebook/jest to detect if Worker instances are keeping the event loop active correctly. Fixes: nodejs#42091 Refs: mafintosh/why-is-node-running#59 Signed-off-by: Darshan Sen <[email protected]>
This should help projects like https://github.com/mafintosh/why-is-node-running and https://github.com/facebook/jest to detect if Worker instances are keeping the event loop active correctly. Fixes: #42091 Refs: mafintosh/why-is-node-running#59 Signed-off-by: Darshan Sen <[email protected]> PR-URL: #42756 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
I tried this in the nightly now ( |
@SimenB I'm assuming that happens after you merged jestjs/jest#12705? That happens because the unref() method on the Worker object doesn't synchronously unref the associated MessagePorts. You may create a new issue for this. If you ignore the const { MessageChannel } = require('worker_threads');
const { port1 } = new MessageChannel();
port1.on('message', () => {}); // You would need to call `port1.unref()` to stop the event loop. But I guess Jest users won't even notice it because |
After that, yes 👍 would waiting for some time help the message channel be closed? |
nope, it just stays open forever unless you do something to close it |
Aha. But if it doesn't keep the process open, I think we can ignore it? Or would your example of |
Actually it does keep the process running. |
Oh 😅 how would you recommend we deal with this? |
We have 2 options here:
I would recommend option 1 if the feature is not used very much and option 2 if it is used a lot. :P |
Sorry, I misunderstood the default behavior. Jest reports handles as open by default, so excluding MessagePort would definitely be noticeable. So scratch option 1, option 2 is better IMO. |
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: nodejs#42091 (comment) Signed-off-by: Darshan Sen <[email protected]>
Great, thanks! |
This should help projects like https://github.com/mafintosh/why-is-node-running and https://github.com/facebook/jest to detect if Worker instances are keeping the event loop active correctly. Fixes: nodejs#42091 Refs: mafintosh/why-is-node-running#59 Signed-off-by: Darshan Sen <[email protected]> PR-URL: nodejs#42756 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
This should help projects like https://github.com/mafintosh/why-is-node-running and https://github.com/facebook/jest to detect if Worker instances are keeping the event loop active correctly. Fixes: #42091 Refs: mafintosh/why-is-node-running#59 Signed-off-by: Darshan Sen <[email protected]> PR-URL: #42756 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
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 <[email protected]> PR-URL: #42849 Reviewed-By: Anna Henningsen <[email protected]>
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 <[email protected]> PR-URL: #42849 Reviewed-By: Anna Henningsen <[email protected]>
@RaisinTen can confirm the jest detection works correctly on 18.1.0, thank you! |
This should help projects like https://github.com/mafintosh/why-is-node-running and https://github.com/facebook/jest to detect if Worker instances are keeping the event loop active correctly. Fixes: #42091 Refs: mafintosh/why-is-node-running#59 Signed-off-by: Darshan Sen <[email protected]> PR-URL: #42756 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
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 <[email protected]> PR-URL: #42849 Reviewed-By: Anna Henningsen <[email protected]>
This should help projects like https://github.com/mafintosh/why-is-node-running and https://github.com/facebook/jest to detect if Worker instances are keeping the event loop active correctly. Fixes: #42091 Refs: mafintosh/why-is-node-running#59 Signed-off-by: Darshan Sen <[email protected]> PR-URL: #42756 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
This should help projects like https://github.com/mafintosh/why-is-node-running and https://github.com/facebook/jest to detect if Worker instances are keeping the event loop active correctly. Fixes: #42091 Refs: mafintosh/why-is-node-running#59 Signed-off-by: Darshan Sen <[email protected]> PR-URL: #42756 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
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 <[email protected]> PR-URL: #42849 Reviewed-By: Anna Henningsen <[email protected]>
This should help projects like https://github.com/mafintosh/why-is-node-running and https://github.com/facebook/jest to detect if Worker instances are keeping the event loop active correctly. Fixes: #42091 Refs: mafintosh/why-is-node-running#59 Signed-off-by: Darshan Sen <[email protected]> PR-URL: #42756 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
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 <[email protected]> PR-URL: #42849 Reviewed-By: Anna Henningsen <[email protected]>
This should help projects like https://github.com/mafintosh/why-is-node-running and https://github.com/facebook/jest to detect if Worker instances are keeping the event loop active correctly. Fixes: nodejs/node#42091 Refs: mafintosh/why-is-node-running#59 Signed-off-by: Darshan Sen <[email protected]> PR-URL: nodejs/node#42756 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
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: nodejs/node#42091 (comment) Signed-off-by: Darshan Sen <[email protected]> PR-URL: nodejs/node#42849 Reviewed-By: Anna Henningsen <[email protected]>
What is the problem this feature will solve?
Jest has a
--detect-open-handles
flag which attempts to figure out (usingasync_hooks
) what resources (timer/server etc.) are preventing a test run/node from exiting. To avoid false positives we perform filtering before presenting the list to the user. One of those things is to check if aTimer
has beenunrefed
or not, viaTimer.hasRef
.However,
Worker
s have nohasRef
, even though they have the{un}ref
pair, so Jest will currently print false positives forWorker
s that have hadunref
called.What is the feature you are proposing to solve the problem?
Add
Worker.hasRef
similar to theTimer
API.What alternatives have you considered?
No response
The text was updated successfully, but these errors were encountered: