Skip to content

Commit

Permalink
#15215 Reduce memory leak in node env by fully uninstalling source-ma…
Browse files Browse the repository at this point in the history
…p-support
  • Loading branch information
eyalroth committed Aug 3, 2024
1 parent 213c3b2 commit 338243a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@

- `[*]` [**BREAKING**] Bundle all of Jest's modules into `index.js` ([#12348](https://github.com/jestjs/jest/pull/12348), [#14550](https://github.com/jestjs/jest/pull/14550) & [#14661](https://github.com/jestjs/jest/pull/14661))
- `[jest-haste-map]` Only spawn one process to check for `watchman` installation ([#14826](https://github.com/jestjs/jest/pull/14826))
- `[jest-runner]` Better cleanup `source-map-support` after test to resolve (minor) memory leak ([#15233](https://github.com/jestjs/jest/pull/15233))

### Chore & Maintenance

Expand Down
19 changes: 15 additions & 4 deletions packages/jest-runner/src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
*/

import {runInContext} from 'node:vm';
import chalk = require('chalk');
import * as fs from 'graceful-fs';
import sourcemapSupport = require('source-map-support');
Expand Down Expand Up @@ -208,6 +209,14 @@ async function runTestInternal(
const tearDownEnv = async () => {
if (!isTornDown) {
runtime.teardown();

// source-map-support keeps memory leftovers in `Error.prepareStackTrace`
runInContext(
"Error.prepareStackTrace = () => '';",
environment.getVmContext()!,
);
sourcemapSupport.resetRetrieveHandlers();

await environment.teardown();
isTornDown = true;
}
Expand Down Expand Up @@ -312,8 +321,12 @@ async function runTestInternal(
sendMessageToJest,
);
} catch (error: any) {
// Access stack before uninstalling sourcemaps
error.stack;
// Access all stacks before uninstalling sourcemaps
let e = error;
while (typeof e === 'object' && e !== null && 'stack' in e) {
e.stack;
e = e?.cause;
}

throw error;
} finally {
Expand Down Expand Up @@ -377,8 +390,6 @@ async function runTestInternal(
});
} finally {
await tearDownEnv();

sourcemapSupport.resetRetrieveHandlers();
}
}

Expand Down

0 comments on commit 338243a

Please sign in to comment.