Skip to content

Commit 8c3f650

Browse files
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 017beed commit 8c3f650

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎.github/workflows/test.yml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,9 @@ jobs:
337337
# patch here (Bun only) rather than via a repo-wide postinstall. The
338338
# jest-runner patch tolerates Bun's transient memoryUsage failures. The
339339
# 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.
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.
341343
- if: matrix.runtime == 'bun'
342344
run: |
343345
git apply test/patches/jest-worker+30.4.1.patch

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
diff --git a/node_modules/jest-worker/build/index.js b/node_modules/jest-worker/build/index.js
2+
index a2fb689..e333058 100644
23
--- a/node_modules/jest-worker/build/index.js
34
+++ b/node_modules/jest-worker/build/index.js
4-
@@ -1345,7 +1345,13 @@
5+
@@ -1345,7 +1345,13 @@ class ExperimentalWorker extends _WorkerAbstract.default {
56
}
67
if (limit && this._childIdleMemoryUsage && this._childIdleMemoryUsage > limit) {
78
this.state = _types.WorkerStates.RESTARTING;
@@ -17,7 +18,7 @@ diff --git a/node_modules/jest-worker/build/index.js b/node_modules/jest-worker/
1718
}
1819
}
1920
diff --git a/node_modules/jest-worker/build/threadChild.js b/node_modules/jest-worker/build/threadChild.js
20-
index e757511..4be460e 100644
21+
index e757511..55ec9be 100644
2122
--- a/node_modules/jest-worker/build/threadChild.js
2223
+++ b/node_modules/jest-worker/build/threadChild.js
2324
@@ -6,6 +6,13 @@
@@ -34,3 +35,16 @@ index e757511..4be460e 100644
3435
/******/ (() => { // webpackBootstrap
3536
/******/ "use strict";
3637
/******/ 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)