Skip to content

Commit

Permalink
Fix a memory leak in Error objects
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Sep 12, 2018
1 parent fa68244 commit 030c969
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion packages/jest-circus/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,16 @@ describe.skip = (blockName: BlockName, blockFn: BlockFn) =>
_dispatchDescribe(blockFn, blockName, 'skip');

const _dispatchDescribe = (blockFn, blockName, mode?: BlockMode) => {
const asyncError = new Error();

// Without this line v8 stores references to all closures
// in the stack in the Error object. This line stringifies the stack
// property to allow garbage-collecting objects on the stack
// https://crbug.com/v8/7142
asyncError.stack = asyncError.stack;

dispatch({
asyncError: new Error(),
asyncError,
blockName,
mode,
name: 'start_describe_definition',
Expand All @@ -48,6 +56,13 @@ const _addHook = (fn: HookFn, hookType: HookType, hookFn, timeout: ?number) => {
if (Error.captureStackTrace) {
Error.captureStackTrace(asyncError, hookFn);
}

// Without this line v8 stores references to all closures
// in the stack in the Error object. This line stringifies the stack
// property to allow garbage-collecting objects on the stack
// https://crbug.com/v8/7142
asyncError.stack = asyncError.stack;

dispatch({asyncError, fn, hookType, name: 'add_hook', timeout});
};

Expand Down Expand Up @@ -81,6 +96,12 @@ const test = (testName: TestName, fn: TestFn, timeout?: number) => {
Error.captureStackTrace(asyncError, test);
}

// Without this line v8 stores references to all closures
// in the stack in the Error object. This line stringifies the stack
// property to allow garbage-collecting objects on the stack
// https://crbug.com/v8/7142
asyncError.stack = asyncError.stack;

return dispatch({
asyncError,
fn,
Expand All @@ -96,6 +117,12 @@ test.skip = (testName: TestName, fn?: TestFn, timeout?: number) => {
Error.captureStackTrace(asyncError, test);
}

// Without this line v8 stores references to all closures
// in the stack in the Error object. This line stringifies the stack
// property to allow garbage-collecting objects on the stack
// https://crbug.com/v8/7142
asyncError.stack = asyncError.stack;

return dispatch({
asyncError,
fn,
Expand All @@ -111,6 +138,12 @@ test.only = (testName: TestName, fn: TestFn, timeout?: number) => {
Error.captureStackTrace(asyncError, test);
}

// Without this line v8 stores references to all closures
// in the stack in the Error object. This line stringifies the stack
// property to allow garbage-collecting objects on the stack
// https://crbug.com/v8/7142
asyncError.stack = asyncError.stack;

return dispatch({
asyncError,
fn,
Expand Down

0 comments on commit 030c969

Please sign in to comment.