Skip to content

Commit

Permalink
Fix a memory leak in Error objects (#6965)
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra authored and SimenB committed Sep 15, 2018
1 parent 8b1be33 commit a66a571
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Fixes

- `[jest-haste-map]` [**BREAKING**] Replaced internal data structures to improve performance ([#6960](https://github.com/facebook/jest/pull/6960))
- `[jest-jasmine2]` Fix memory leak in Error objects hold by the framework ([#6965](https://github.com/facebook/jest/pull/6965))

### Chore & Maintenance

Expand Down
6 changes: 6 additions & 0 deletions packages/jest-jasmine2/src/jasmine/Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ export default function Spec(attrs: Object) {
this.initError = new Error();
this.initError.name = '';

// 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
this.initError.stack = this.initError.stack;

this.queueableFn.initError = this.initError;

this.result = {
Expand Down
12 changes: 12 additions & 0 deletions packages/jest-jasmine2/src/jasmine_async.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ function promisifyLifeCycleFunction(originalFn, env) {

const extraError = 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
extraError.stack = extraError.stack;

// We make *all* functions async and run `done` right away if they
// didn't return a promise.
const asyncJestLifecycle = function(done) {
Expand Down Expand Up @@ -79,6 +85,12 @@ function promisifyIt(originalFn, env) {

const extraError = 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
extraError.stack = extraError.stack;

const asyncJestTest = function(done) {
const wrappedFn = isGeneratorFn(fn) ? co.wrap(fn) : fn;
const returnValue = wrappedFn.call({});
Expand Down
1 change: 1 addition & 0 deletions packages/jest-jasmine2/src/queue_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export default function queueRunner(options: Options) {
'Timeout - Async callback was not invoked within the ' +
timeoutMs +
'ms timeout specified by jest.setTimeout.';
initError.stack = initError.message + initError.stack;
options.onException(initError);
},
);
Expand Down

0 comments on commit a66a571

Please sign in to comment.