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 memory leaks of jest-worker #11187

Merged
merged 5 commits into from
Mar 15, 2021
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 @@ -64,6 +64,7 @@
- `[jest-worker]` Handle `ERR_IPC_CHANNEL_CLOSED` errors properly ([#11143](https://github.com/facebook/jest/pull/11143))
- `[pretty-format]` [**BREAKING**] Convert to ES Modules ([#10515](https://github.com/facebook/jest/pull/10515))
- `[pretty-format]` Only call `hasAttribute` if it's a function ([#11000](https://github.com/facebook/jest/pull/11000))
- `[jest-worker]` Fix memory leaks of jest-worker ([#11187](https://github.com/facebook/jest/pull/11187))

### Chore & Maintenance

Expand Down
17 changes: 14 additions & 3 deletions packages/jest-worker/src/Farm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,14 @@ export default class Farm {
};

const promise: PromiseWithCustomMessage<unknown> = new Promise(
(resolve, reject) => {
// Bind args to this function so it won't reference to the parent scope.
// This prevents a memory leak in v8, because otherwise the function will
// retaine args for the closure.
((
args: Array<unknown>,
resolve: (value: unknown) => void,
reject: (reason?: any) => void,
) => {
const computeWorkerKey = this._computeWorkerKey;
const request: ChildMessage = [CHILD_MESSAGE_CALL, false, method, args];

Expand Down Expand Up @@ -101,7 +108,7 @@ export default class Farm {
} else {
this._push(task);
}
},
}).bind(null, args),
);

promise.UNSTABLE_onCustomMessage = addCustomMessageListener;
Expand All @@ -124,8 +131,12 @@ export default class Farm {
throw new Error('Queue implementation returned processed task');
}

// Reference the task object outside so it won't be retained by onEnd,
// and other properties of the task object, such as task.request can be
// garbage collected.
const taskOnEnd = task.onEnd;
const onEnd = (error: Error | null, result: unknown) => {
task.onEnd(error, result);
taskOnEnd(error, result);

this._unlock(workerId);
this._process(workerId);
Expand Down
10 changes: 8 additions & 2 deletions packages/jest-worker/src/workers/NodeThreadsWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,21 @@ export default class ExperimentalWorker implements WorkerInterface {
send(
request: ChildMessage,
onProcessStart: OnStart,
onProcessEnd: OnEnd,
onProcessEnd: OnEnd | null,
onCustomMessage: OnCustomMessage,
): void {
onProcessStart(this);
this._onProcessEnd = (...args) => {
// Clean the request to avoid sending past requests to workers that fail
// while waiting for a new request (timers, unhandled rejections...)
this._request = null;
return onProcessEnd(...args);

const res = onProcessEnd?.(...args);

// Clean up the reference so related closures can be garbage collected.
onProcessEnd = null;

return res;
};

this._onCustomMessage = (...arg) => onCustomMessage(...arg);
Expand Down