Skip to content

Commit 1313ca6

Browse files
authored
process.exit(): throw process.reallyExit is not a function, pass process as this (#44057)
### Problem - `process.exit()` calls `process.reallyExit(code)`, as Node.js does. When `process.reallyExit` is not callable, Bun throws a `TypeError` with an empty message. Node.js throws `TypeError: process.reallyExit is not a function`. - A build with assertions aborts: `ASSERTION FAILED: !message.isEmpty()` at `vendor/WebKit/Source/JavaScriptCore/runtime/Error.cpp(71)` in `JSC::createTypeError`. - The cause is `JSC::call(globalObject, reallyExitVal, args, ""_s)` at `src/jsc/bindings/BunProcess.cpp:913`. The last argument is the message for a value that is not callable. This overload also passes the function as its own `this`. Node.js passes `process`. ### Fix - The call passes `process` as `this` and the message `process.reallyExit is not a function`. - Correct because Node.js runs `process.reallyExit(process.exitCode || 0)` in JavaScript. That is a method call on `process`, and V8 gives that message. - Verified: two new tests in `test/js/node/process/process.test.js`. Both fail on release 1.4.3-canary.1. Also ran the other `process.exit()` tests and 12 exit tests from `test/js/node/test/parallel/`. ### Background - `process.reallyExit` is the raw exit of Node.js: it emits no `'exit'` event. Libraries such as signal-exit replace it. - `JSC::call` is the JavaScriptCore helper that calls a JS function from C++. - No other design applies: the two wrong values are arguments of one call. No other `JSC::call` in `src/` has an empty message. ### Downsides - A function that replaces `process.reallyExit` now sees `this === process` when `process.exit()` calls it. Before, `this` was the function itself. - Size: +53 bytes in the `BunProcess.cpp` object (`-O3`, no LTO): 37 for the message, 16 of code. Per call: no new allocation, branch, or syscall. <details><summary>Notes</summary> Repro: ```js process.reallyExit = "str"; process.exit(0); ``` | | stderr | exit code | |---|---|---| | Bun 1.4.3-canary.1+367d939d9 (release) | `TypeError: ` | 1 | | Bun 1.4.3-debug+367d939d9 | `ASSERTION FAILED: !message.isEmpty()` | 134 | | this branch (debug) | `TypeError: process.reallyExit is not a function` | 1 | | Node.js v26.3.0 | `TypeError: process.reallyExit is not a function` | 0 | The same result for each value tried: `"str"`, `undefined`, `null`, `1`, `{}`, `Symbol("x")`, and `delete process.reallyExit`. The `this` value, with `process.reallyExit = function () { console.log(this === process) }` and `process.exit(3)`: `false` before, `true` on this branch and on Node.js v26.3.0. Not changed by this PR: when nothing catches the `TypeError`, Node.js exits with the code given to `process.exit()` (0 above, 5 for `process.exit(5)`). Bun exits 1. A replaced `process.reallyExit` that throws shows the same difference. For an entry point that throws, `uncaught_exception` (`src/jsc/VirtualMachine.rs:2161`) and `exit_with_unhandled_note` (`src/runtime/cli/run_command.rs:1638`) both set exit code 1. Related to #42032, which changes the first place for an `'exit'` listener that throws. The new test catches the error in the child, so it does not depend on that exit code. History: #16026 added the call with the comment `// process.reallyExit(exitCode);`. Search for other call sites: `JSC::call`, `call`, `profiledCall` and `construct` with an empty message literal in `src/` and `packages/`. `BunProcess.cpp:913` is the only one. Size measurement: compiled `BunProcess.cpp` from `origin/main` and from this branch with the release flags of the build (`-O3 -march=nehalem`), without `-flto=thin` so that the object is native code. `size -A`: `.text` of `Bun::Process_functionExit` 712 to 728 bytes, `.rodata.str1.1` 10594 to 10631 bytes, `.data` and `.bss` unchanged. On a debug build without the fix, the first new test fails: the child exits with code 134 and the assertion is in its stderr. Tests run on the debug build of this branch: - `test/js/node/process/process.test.js -t "process.exit\(\)"`: 6 pass. - `test/js/node/process/process.test.js`, whole file: 179 pass, 5 skip, 3 fail. `process` fails because the container sets no `USER`. `signal > simple case works` and `signal > process.emit will call signal events` time out at 5 s in the whole-file run, with and without the fix. Each passes in 2.5 s when it runs alone. - `test/js/node/test/parallel/`: `test-process-exit-code-validation.js`, `test-process-exit-code.js`, `test-process-exit-from-before-exit.js`, `test-process-exit-handler.js`, `test-process-exit-recursive.js`, `test-process-exit.js`, `test-process-really-exit.js`, `test-worker-exit-code.js`, `test-worker-nested-on-process-exit.js`, `test-worker-on-process-exit.js`, `test-worker-process-exit-async-module.js`, `test-crypto-op-during-process-exit.js`: all exit 0. Not run: macOS, Windows. The changed line has no platform-specific code. </details> <!-- robobun:evidence:begin --> --- **no test proof** · iteration 0 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/js/node/process/process.test.js <!-- robobun:evidence:end -->
1 parent 91c3182 commit 1313ca6

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎src/jsc/bindings/BunProcess.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionExit, (JSC::JSGlobalObject * globalObje
910910
RETURN_IF_EXCEPTION(throwScope, {});
911911
MarkedArgumentBuffer args;
912912
args.append(jsNumber(Bun__getExitCode(bunVM(zigGlobal))));
913-
JSC::call(globalObject, reallyExitVal, args, ""_s);
913+
JSC::call(globalObject, reallyExitVal, process, args, "process.reallyExit is not a function"_s);
914914
RETURN_IF_EXCEPTION(throwScope, {});
915915

916916
return JSC::JSValue::encode(jsUndefined());

‎test/js/node/process/process.test.js‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2574,6 +2574,56 @@ describe.concurrent("process.exit()", () => {
25742574
exitCode: 42,
25752575
});
25762576
});
2577+
2578+
it("throws a TypeError with a message when process.reallyExit is not callable", async () => {
2579+
await using proc = Bun.spawn({
2580+
cmd: [
2581+
bunExe(),
2582+
"-e",
2583+
`const results = [];
2584+
for (const value of ["str", undefined, {}]) {
2585+
process.reallyExit = value;
2586+
try {
2587+
process.exit(0);
2588+
results.push("did not throw");
2589+
} catch (e) {
2590+
results.push({ isTypeError: e instanceof TypeError, message: e.message });
2591+
}
2592+
}
2593+
console.log(JSON.stringify(results));`,
2594+
],
2595+
env: bunEnv,
2596+
stdout: "pipe",
2597+
stderr: "pipe",
2598+
});
2599+
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
2600+
const expected = { isTypeError: true, message: "process.reallyExit is not a function" };
2601+
expect({ stdout, stderr, exitCode }).toEqual({
2602+
stdout: JSON.stringify([expected, expected, expected]) + "\n",
2603+
stderr: "",
2604+
exitCode: 0,
2605+
});
2606+
});
2607+
2608+
it("calls process.reallyExit as a method of process", async () => {
2609+
await using proc = Bun.spawn({
2610+
cmd: [
2611+
bunExe(),
2612+
"-e",
2613+
`const reallyExit = process.reallyExit;
2614+
process.reallyExit = function (code) {
2615+
require("node:fs").writeSync(1, "this is process: " + (this === process) + "\\n");
2616+
return reallyExit.call(process, code);
2617+
};
2618+
process.exit(3);`,
2619+
],
2620+
env: bunEnv,
2621+
stdout: "pipe",
2622+
stderr: "pipe",
2623+
});
2624+
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
2625+
expect({ stdout, stderr, exitCode }).toEqual({ stdout: "this is process: true\n", stderr: "", exitCode: 3 });
2626+
});
25772627
});
25782628

25792629
it("process.memoryUsage.arrayBuffers", () => {

0 commit comments

Comments
 (0)