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

Improve detectOpenHandles #13417

Merged
merged 10 commits into from Sep 19, 2023
Merged

Improve detectOpenHandles #13417

merged 10 commits into from Sep 19, 2023

Conversation

liuxingbaoyu
Copy link
Contributor

Summary

Removed some previously ignored detection types.

Ref: #13414

Test plan

CI GREEN

Comment on lines 85 to +86
// tracked.
if (
type === 'PROMISE' ||
type === 'TIMERWRAP' ||
type === 'ELDHISTOGRAM' ||
type === 'PerformanceObserver' ||
type === 'RANDOMBYTESREQUEST' ||
type === 'DNSCHANNEL' ||
type === 'ZLIB' ||
type === 'SIGNREQUEST'
) {
if (type === 'PROMISE') {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Only TIMERWRAP was added before jest repo was migrated to ts, it's a bit troublesome to find commit records, I don't have confirmation.
For other types, I have checked the submitted PRs and tests, and they are theoretically safe.
Anyway, this is in a major release and can be safely tested in a beta release.

Copy link
Member

Choose a reason for hiding this comment

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

include PerformanceObserver to fix CI?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me try to destroy it. #13417 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

Original issue with a reproduction: #11051

If node doesn't require a disconnect to exit cleanly, we shouldn't report it

Copy link
Member

Choose a reason for hiding this comment

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

We can probably add

const client = require("prom-client");
client.collectDefaultMetrics();

as an integration test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not really sure what detectOpenHandles is for now, technically most of the object types removed by this PR should not block process exit, but it seems that some users use this to detect memory leaks/object leaks. Would love to hear your opinion.

Copy link
Member

Choose a reason for hiding this comment

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

It's used to detect handles keeping the process running after tests complete (such as a rogue server, setInterval (without unref() or clearInterval)) etc.. It shouldn't be used for memory leaks per se (although open handles indicate a missing cleanup of some sorts), but since it forces GC runs it oftens helps with memory usage

Copy link
Member

Choose a reason for hiding this comment

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

essentially to find out why https://jestjs.io/docs/cli#--forceexit would be needed

Comment on lines 13 to 19
exports[`prints out info about open handlers 1`] = `
"Jest has detected the following 1 open handle potentially keeping Jest from exiting:

● TCPSERVERWRAP
DNSCHANNEL,TCPSERVERWRAP

12 | const app = new Server();
13 |
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I modified the logic here and now it will report all types at the same stack.
Not sure if the prompt message needs to be modified, but I'm not good at English and can't do anything about it.🤦‍♂️

Copy link
Member

Choose a reason for hiding this comment

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

Awesome change! We could probably land this on main?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I can do this, but the tests in the main branch don't reflect this. I couldn't think of a test example like this for a while. (DNSCHANNEL is ignored in main branch)

Comment on lines 144 to 138
await asyncSleep(100);
await asyncSleep(0);

if (activeHandles.size > 0) {
// For some special objects such as `TLSWRAP`.
// Ref: https://github.com/facebook/jest/issues/11665
runGC();
await asyncSleep(30);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Checking length is free, let's check it first. Another 100ms might be a bit long? I modified it to 30ms, you can also modify it to 0-10ms if you want, lol.

Copy link
Member

Choose a reason for hiding this comment

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

as low as possible while still passing all tests (without introducing flake) seems ideal 😀

@liuxingbaoyu
Copy link
Contributor Author

liuxingbaoyu commented Oct 10, 2022

The failure in CI I should know how to fix, but I can't reproduce it locally (win) anyway, which is weird. Even I can't reproduce it using gitpod (linux). 😕

Seems to be related to node version, node18 on CI passes, node16 fails while failure is expected.

Yes, this seems to be an undocumented behavior, there is no such type PerformanceObserver in the docs for both node16 and node18.

@SimenB SimenB added this to the Jest 30 milestone Oct 10, 2022
@legobeat
Copy link

legobeat commented May 1, 2023

CI error seems resolved

@SimenB
Copy link
Member

SimenB commented May 12, 2023

/easycla

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented May 12, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

@netlify
Copy link

netlify bot commented Sep 19, 2023

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit e569539
🔍 Latest deploy log https://app.netlify.com/sites/jestjs/deploys/6509c09d56b81c00089c9537
😎 Deploy Preview https://deploy-preview-13417--jestjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@SimenB
Copy link
Member

SimenB commented Sep 19, 2023

Hey @liuxingbaoyu, finally got back to this 😀 Could you sign the CLA? 🙂

@liuxingbaoyu
Copy link
Contributor Author

Sorry for forgetting this!
Of course, it's done!

@SimenB
Copy link
Member

SimenB commented Sep 19, 2023

Wonderful, thanks!

@SimenB SimenB enabled auto-merge (squash) September 19, 2023 11:52
@SimenB SimenB changed the title [jest-30] Improve detectOpenHandles Improve detectOpenHandles Sep 19, 2023
@SimenB SimenB merged commit 4727f80 into jestjs:main Sep 19, 2023
62 checks passed
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 20, 2023
@SimenB
Copy link
Member

SimenB commented Oct 30, 2023

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants