Skip to content

Unref'd timeouts should not be reported as open handles #8939

@pimterry

Description

@pimterry

🐛 Bug Report

Timeout.unref() in node tells node not to let the timeout block process shutdown. That means that these timeouts are never going to stop Jest from shutting down, but --detectOpenHandles still reports them as such.

Any intervals or timeouts that has been unref'd should not be reported by Jest as an open handle.

I'm not actually a Jest user myself - I'm reporting this as I received a bug report on a testing library I maintain, because Jest is complaining about an unref'd interval used here.

To Reproduce

In any test, include an unref'd timer:

const interval = setInterval(() => {}, 10000);
interval.unref();

This test will exit happily with no problems.

Run this test without the unref() line though and it won't, and you'll see a Jest did not exit one second after the test run has completed warning.

However, if you run this test with --detectOpenHandles then regardless of unref(), this setInterval will always be reported as an open handle.

Expected behavior

--detectOpenHandles should not show a warning for this interval, but it does.

Suggestion

Since Node v11, there's a Timeout.hasRef() function, which can be used to check whether any timeout will keep the event loop active, or whether it's unref'd and will allow shutdown.

I'd be happy to put together a quick PR to fix this, if it's likely to be accepted? I think this is pretty easy to solve for node 11+: just filter the active timeout handles by the result of hasRef(), if present.

You could polyfill hasRef() for pre-v11 versions, but v12 is going to be the active LTS release in a month anyway, and this isn't a blocking issue, so I probably wouldn't bother.

How does that sound?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions