Skip to content

Commit 6412396

Browse files
ci(bun): restore the worker idle memory limit and end a Bun worker from itself (#21646)
* ci(bun): restore the worker idle memory limit The limit bounds each worker thread's heap; without it the suite grows until the runner is out of memory. Dropping it in #21629 traded Bun's segfault for an OOM. * ci(bun): end a Bun worker from itself instead of terminating it Bun segfaults when jest terminates a worker thread from the parent on an idle-memory restart, a different wild address each time and workers_terminated short of workers_spawned. Sending the end message jest already uses for shutdown lets the worker leave on its own. Guarded on Bun, so Node and Deno still terminate. Reproduced with a low limit: terminate crashes 5/5, ending 3/3 passes. * ci(bun): exit a Bun worker once it has ended, so the pool replaces it Ending a worker only removes its message listener; the thread leaves only if that was its last handle. A jest worker that leaked one therefore stayed alive in the RESTARTING state forever — reproduced with a worker holding a timer: the idle-memory limit tripped (1.63 MB over a 1 MB limit), the restart was requested, and the next task still ran on the same thread. The limit had become a no-op for that worker, which is the one thing it exists to do. The worker now exits itself after teardown, on Bun only. Same reproduction: the state returns to `ok` and the next task runs on a new thread.
1 parent 1b246ec commit 6412396

3 files changed

Lines changed: 39 additions & 6 deletions

File tree

‎.github/workflows/test.yml‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,11 @@ jobs:
335335
# OOMs the runner on the full suite and Bun's child_process workers hang, but
336336
# threads work once jest-worker loads the Bun preload per thread. Apply that
337337
# patch here (Bun only) rather than via a repo-wide postinstall. The
338-
# jest-runner patch tolerates Bun's transient memoryUsage failures.
339-
# test:base:bun also drops --workerIdleMemoryLimit: restarting a worker
340-
# thread on it segfaults Bun, and the runner's heaps sit at the 512MB the
341-
# other runtimes use, so it fired constantly.
338+
# jest-runner patch tolerates Bun's transient memoryUsage failures. The
339+
# jest-worker patch also has a Bun worker end itself rather than be
340+
# terminated from the parent, which segfaults Bun on an idle-memory restart
341+
# — and exit once it has, since ending only drops its message listener and
342+
# a worker still holding a handle would never be replaced.
342343
- if: matrix.runtime == 'bun'
343344
run: |
344345
git apply test/patches/jest-worker+30.4.1.patch

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"test:deno": "yarn test:base:deno --reporters default --reporters \"<rootDir>/test/runtimeCrashReporter.js\" --testMatch \"<rootDir>/test/*.{basictest,longtest,test,unittest}.js\" --testPathIgnorePatterns \"/node_modules/\" \"(ProfilingPlugin[.]unittest|SyntaxBrowserEquivalence|WebpackDevServer)\"",
7171
"test:base:deno": "deno --allow-read --allow-env --allow-sys --allow-ffi --allow-write --allow-run --allow-net --import-map=test/deno-import-map.json --v8-flags='--max-old-space-size=4096' ./node_modules/jest-cli/bin/jest.js --workerIdleMemoryLimit=512MB --logHeapUsage",
7272
"test:bun": "yarn test:base:bun --reporters default --reporters \"<rootDir>/test/runtimeCrashReporter.js\" --testMatch \"<rootDir>/test/*.{basictest,longtest,test,unittest}.js\" --testPathIgnorePatterns \"/node_modules/\" \"(ProfilingPlugin[.]unittest|SyntaxBrowserEquivalence|WebpackDevServer)\"",
73-
"test:base:bun": "bun --bun --smol --preload ./test/bun-preload.js ./node_modules/jest-cli/bin/jest.js --workerThreads --logHeapUsage",
73+
"test:base:bun": "bun --bun --smol --preload ./test/bun-preload.js ./node_modules/jest-cli/bin/jest.js --workerThreads --workerIdleMemoryLimit=512MB --logHeapUsage",
7474
"test:update-snapshots": "yarn test:base -u",
7575
"test:size": "node --max-old-space-size=4096 ./test/CodeSizeTestCases.size.js",
7676
"report:cover": "nyc report --reporter=lcov --reporter=text -t coverage",

‎test/patches/jest-worker+30.4.1.patch‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1+
diff --git a/node_modules/jest-worker/build/index.js b/node_modules/jest-worker/build/index.js
2+
index a2fb689..e333058 100644
3+
--- a/node_modules/jest-worker/build/index.js
4+
+++ b/node_modules/jest-worker/build/index.js
5+
@@ -1345,7 +1345,13 @@ class ExperimentalWorker extends _WorkerAbstract.default {
6+
}
7+
if (limit && this._childIdleMemoryUsage && this._childIdleMemoryUsage > limit) {
8+
this.state = _types.WorkerStates.RESTARTING;
9+
- this._worker.terminate();
10+
+ // webpack: Bun segfaults when a worker thread is terminated from the
11+
+ // parent, so ask the worker to end itself. No-op off Bun.
12+
+ if (process.versions.bun) {
13+
+ this._worker.postMessage([_types.CHILD_MESSAGE_END, false]);
14+
+ } else {
15+
+ this._worker.terminate();
16+
+ }
17+
}
18+
}
19+
}
120
diff --git a/node_modules/jest-worker/build/threadChild.js b/node_modules/jest-worker/build/threadChild.js
2-
index e757511..4be460e 100644
21+
index e757511..55ec9be 100644
322
--- a/node_modules/jest-worker/build/threadChild.js
423
+++ b/node_modules/jest-worker/build/threadChild.js
524
@@ -6,6 +6,13 @@
@@ -16,3 +35,16 @@ index e757511..4be460e 100644
1635
/******/ (() => { // webpackBootstrap
1736
/******/ "use strict";
1837
/******/ var __webpack_modules__ = ({
38+
@@ -309,6 +316,12 @@ function end() {
39+
function exitProcess() {
40+
// Clean up open handles so the worker ideally exits gracefully
41+
_nodeWorker_threads().parentPort.removeListener('message', messageListener);
42+
+ // webpack: on Bun the parent asks the worker to end rather than terminating
43+
+ // it, so the thread has to leave for real — a handle it still holds would
44+
+ // otherwise keep it alive and the pool would never replace it.
45+
+ if (process.versions.bun) {
46+
+ process.exit(0);
47+
+ }
48+
}
49+
function execMethod(method, args) {
50+
const main = require(file);

0 commit comments

Comments
 (0)