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 detectOpenHandles false positives for some special objects such as TLSWRAP #13414

Merged
merged 5 commits into from Oct 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -14,6 +14,7 @@
- `[jest-reporters]` Revert: Transform file paths into hyperlinks ([#13399](https://github.com/facebook/jest/pull/13399))
- `[@jest/types]` Infer type of `each` table correctly when the table is a tuple or array ([#13381](https://github.com/facebook/jest/pull/13381))
- `[@jest/types]` Rework typings to allow the `*ReturnedWith` matchers to be called with no argument ([#13385](https://github.com/facebook/jest/pull/13385))
- `[jest-core]` Fix `detectOpenHandles` false positives for some special objects such as `TLSWRAP`. ([#13414](https://github.com/facebook/jest/pull/13414))
SimenB marked this conversation as resolved.
Show resolved Hide resolved

### Chore & Maintenance

Expand Down
12 changes: 12 additions & 0 deletions packages/jest-core/src/__tests__/collectHandles.test.js
Expand Up @@ -10,6 +10,7 @@ import * as crypto from 'crypto';
import {promises as dns} from 'dns';
import http from 'http';
import {PerformanceObserver} from 'perf_hooks';
import {TLSSocket} from 'tls';
import zlib from 'zlib';
import collectHandles from '../collectHandles';

Expand Down Expand Up @@ -134,4 +135,15 @@ describe('collectHandles', () => {
expect.objectContaining({message: 'TCPSERVERWRAP'}),
);
});

it('should collect handles for some special objects such as `TLSWRAP`', async () => {
const handleCollector = collectHandles();

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

const openHandles = await handleCollector();

expect(openHandles).toHaveLength(0);
});
});
28 changes: 28 additions & 0 deletions packages/jest-core/src/collectHandles.ts
Expand Up @@ -9,6 +9,8 @@

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

const asyncSleep = promisify(setTimeout);

let gcFunc: Function | undefined = (globalThis as any).gc;
liuxingbaoyu marked this conversation as resolved.
Show resolved Hide resolved
function runGC() {
if (!gcFunc) {
v8.setFlagsFromString('--expose-gc');
gcFunc = vm.runInNewContext('gc');
v8.setFlagsFromString('--no-expose-gc');
if (!gcFunc) {
SimenB marked this conversation as resolved.
Show resolved Hide resolved
console.warn(
'Cannot find `global.gc` function. Please run node with `--expose-gc`',
);
// eslint-disable-next-line @typescript-eslint/no-empty-function
gcFunc = () => {};
}
}

gcFunc();
}

// Inspired by https://github.com/mafintosh/why-is-node-running/blob/master/index.js
// Extracted as we want to format the result ourselves
export default function collectHandles(): HandleCollectionResult {
Expand Down Expand Up @@ -125,6 +145,14 @@ export default function collectHandles(): HandleCollectionResult {
// callback, we will not yet have seen the resource be destroyed here.
await asyncSleep(100);

if (activeHandles.size !== 0) {
liuxingbaoyu marked this conversation as resolved.
Show resolved Hide resolved
// For some special objects such as `TLSWRAP`.
// Ref: https://github.com/facebook/jest/issues/11665
runGC();

await asyncSleep(0);
}

hook.disable();

// Get errors for every async resource still referenced at this moment
Expand Down