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

fix: Revert breaking changes for detectOpenHandles #14789

Merged
merged 5 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- `[@jest/core]` [**BREAKING**] Group together open handles with the same stack trace ([#13417](https://github.com/jestjs/jest/pull/13417), & [#14543](https://github.com/jestjs/jest/pull/14543))
- `[@jest/core]` Add `perfStats` to surface test setup overhead ([#14622](https://github.com/jestjs/jest/pull/14622))
- `[@jest/core]` [**BREAKING**] Changed `--filter` to accept an object with shape `{ filtered: Array<string> }` to match [documentation](https://jestjs.io/docs/cli#--filterfile) ([#13319](https://github.com/jestjs/jest/pull/13319))
- `[@jest/core]` [**BREAKING**] Revert breaking changes for `detectOpenHandles`. ([#14789](https://github.com/jestjs/jest/pull/14789))
Copy link
Member

Choose a reason for hiding this comment

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

should this entry be grouped with the line on line 10?

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, this looks good to me.
Also the original second PR on line 10 seemed to be irrelevant, so I replaced it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We might also be able to remove [**BREAKING**].

- `[@jest/core, @jest/test-sequencer]` [**BREAKING**] Exposes `globalConfig` & `contexts` to `TestSequencer` ([#14535](https://github.com/jestjs/jest/pull/14535), & [#14543](https://github.com/jestjs/jest/pull/14543))
- `[jest-environment-jsdom]` [**BREAKING**] Upgrade JSDOM to v22 ([#13825](https://github.com/jestjs/jest/pull/13825))
- `[@jest/environment-jsdom-abstract]` Introduce new package which abstracts over the `jsdom` environment, allowing usage of custom versions of JSDOM ([#14717](https://github.com/jestjs/jest/pull/14717))
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/detectOpenHandles.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ exports[`prints message about flag on slow tests with a custom timeout 1`] = `
exports[`prints out info about open handlers 1`] = `
"Jest has detected the following 1 open handle potentially keeping Jest from exiting:

DNSCHANNEL,TCPSERVERWRAP
● TCPSERVERWRAP

12 | const app = new Server();
13 |
Expand Down
12 changes: 3 additions & 9 deletions packages/jest-core/src/__tests__/collectHandles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ describe('collectHandles', () => {
it('should not collect the PerformanceObserver open handle', async () => {
const handleCollector = collectHandles();

let obs = new PerformanceObserver((list, observer) => {});
const obs = new PerformanceObserver((list, observer) => {});
obs.observe({entryTypes: ['mark']});
obs.disconnect();
obs = null;

const openHandles = await handleCollector();

Expand All @@ -47,12 +45,9 @@ describe('collectHandles', () => {
it('should not collect the DNSCHANNEL open handle', async () => {
const handleCollector = collectHandles();

let resolver = new dns.Resolver();
const resolver = new dns.Resolver();
resolver.getServers();

// We must drop references to it
resolver = null;

const openHandles = await handleCollector();

expect(openHandles).not.toContainEqual(
Expand Down Expand Up @@ -141,11 +136,10 @@ describe('collectHandles', () => {
);
});

it('should not be false positives for some special objects such as `TLSWRAP`', async () => {
it('should not collect the `TLSWRAP` open handle', async () => {
const handleCollector = collectHandles();

const socket = new TLSSocket();
socket.destroy();

const openHandles = await handleCollector();

Expand Down
35 changes: 17 additions & 18 deletions packages/jest-core/src/collectHandles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

import * as asyncHooks from 'async_hooks';
import {promisify} from 'util';
import * as v8 from 'v8';
import * as vm from 'vm';
import {getHeapSnapshot} from 'v8';
import stripAnsi = require('strip-ansi');
import type {Config} from '@jest/types';
import {formatExecError} from 'jest-message-util';
Expand Down Expand Up @@ -46,20 +45,9 @@ const hasWeakRef = typeof WeakRef === 'function';

const asyncSleep = promisify(setTimeout);

let gcFunc: (() => void) | undefined = (globalThis as any).gc;
function runGC() {
if (!gcFunc) {
v8.setFlagsFromString('--expose-gc');
gcFunc = vm.runInNewContext('gc');
v8.setFlagsFromString('--no-expose-gc');
if (!gcFunc) {
throw new Error(
'Cannot find `global.gc` function. Please run node with `--expose-gc` and report this issue in jest repo.',
);
}
}

gcFunc();
// It is more aggressive than `gc()`.
getHeapSnapshot();
}

// Inspired by https://github.com/mafintosh/why-is-node-running/blob/master/index.js
Expand All @@ -82,7 +70,20 @@ export default function collectHandles(): HandleCollectionResult {
// Skip resources that should not generally prevent the process from
// exiting, not last a meaningfully long time, or otherwise shouldn't be
// tracked.
if (type === 'PROMISE') {
if (
[
'PROMISE',
'TIMERWRAP',
'ELDHISTOGRAM',
'PerformanceObserver',
'RANDOMBYTESREQUEST',
'DNSCHANNEL',
'ZLIB',
'SIGNREQUEST',
'TLSWRAP',
'TCPWRAP',
].includes(type)
) {
return;
}
const error = new ErrorWithStack(type, initHook, 100);
Expand Down Expand Up @@ -136,8 +137,6 @@ export default function collectHandles(): HandleCollectionResult {
await asyncSleep(30);

if (activeHandles.size > 0) {
// For some special objects such as `TLSWRAP`.
// Ref: https://github.com/jestjs/jest/issues/11665
runGC();

await asyncSleep(0);
Expand Down
Loading