Skip to content

child_process: run the requested module when fork() is called inside a compiled executable - #35129

Closed
robobun wants to merge 8 commits into
mainfrom
farm/d14eca12/fork-in-compiled-exe
Closed

robobun wants to merge 8 commits into
mainfrom
farm/d14eca12/fork-in-compiled-exe

Conversation

@robobun

@robobun robobun commented Jul 22, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Inside a bun build --compile binary, child_process.fork(\"./job.mjs\") spawns process.execPath (the compiled binary itself). A standalone executable always booted its embedded main entry point, so ./job.mjs was demoted to process.argv[2] and the forked module never ran. Unguarded, the child re-executed the parent program, which forked again: an exponential self-respawn storm. cluster.fork() hit the same path and leaked the module path into the worker's process.argv.

Repro

cat > main.ts <<'TS'
if (process.env.__DEPTH) { console.log(\"SPAWNED-AS-MAIN depth=\" + process.env.__DEPTH + \" argv=\" + JSON.stringify(process.argv)); process.exit(7); }
import { fork } from \"child_process\";
const p = fork(\"./job.mjs\", [\"a1\"], { silent: true, env: { ...process.env, __DEPTH: \"1\" } });
let out = \"\"; p.stdout?.on(\"data\", d => out += d);
p.on(\"exit\", c => { console.log(\"PARENT: child exit=\" + c + \" out=\" + JSON.stringify(out.trim())); process.exit(0); });
TS
cat > job.mjs <<'JS'
console.log(\"job.mjs RAN args=\" + JSON.stringify(process.argv.slice(2))); process.exit(0);
JS
bun build --compile main.ts job.mjs --outfile app && ./app

Before: PARENT: child exit=7 out=\"SPAWNED-AS-MAIN depth=1 argv=[\\\"bun\\\",\\\"/\$bunfs/root/app\\\",\\\"./job.mjs\\\",\\\"a1\\\"]\"
After: PARENT: child exit=0 out=\"job.mjs RAN args=[\\\"a1\\\"]\"

Cause

boot_standalone in src/runtime/cli/mod.rs hard-codes graph.entry_point() as the module to run and puts all of argv[1..] into ctx.passthrough. There was no mechanism for a compiled binary to be told "run this other embedded module instead". new Worker(path) already had graph-aware resolution, but fork() spawns a fresh process so that path never applied.

Fix

When fork() runs inside a standalone executable and is spawning itself, it sets BUN_INTERNAL_FORK_ENTRY=<modulePath> in the child's environment. The child's boot_standalone reads and unsets it, resolves it against the embedded module graph using the same extension remapping new Worker(path) uses (./job.mjs -> /\$bunfs/root/job.js), and boots that module instead of the baked entry point. Everything in argv after the module path becomes process.argv[2..]. If the module is not embedded, the path is resolved against cwd so on-disk scripts still work; a missing module surfaces as a normal "Module not found" error.

cluster.fork() goes through the same child_process.fork(), so cluster workers now boot with a clean process.argv too.

Verification

bun bd test test/bundler/bundler_compile.test.ts -t \"ChildProcessForkEmbedded|ClusterFork\"

Both new tests fail on the released binary (MAIN-REENTERED argv=[\"./job.mjs\",\"hello\"] / argv=[\"/\$bunfs/root/out\"]) and pass with this change. compile-argv.test.ts, the existing compile/Worker* tests, test/js/node/cluster.test.ts, and the Node-ported test-child-process-fork-* suite continue to pass.


no test proof · iteration 3 · Platform-specific test(s) that do not run on this machine. Deferring to CI, which covers all platforms: test/bundler/bundler_compile.test.ts

…a compiled executable

Inside a bun build --compile binary, child_process.fork(modulePath) spawns
process.execPath (the compiled binary itself), which unconditionally booted
the embedded main entry point and treated modulePath as a passthrough arg.
The forked module never ran, and unguarded forks self-respawned.

fork() now sets BUN_INTERNAL_FORK_ENTRY when spawning itself from a
standalone executable. boot_standalone reads it, resolves it against the
embedded module graph (with the same extension remapping new Worker(path)
uses) and uses that as the entry point, falling back to disk for
non-embedded paths. argv after the module path becomes process.argv[2..]
as fork() callers expect. cluster.fork() goes through the same path.
@robobun

robobun commented Jul 22, 2026 •

Copy link
Copy Markdown
Collaborator Author

Reproduced: fork("./job.mjs") inside a compiled binary re-executes the main entry instead of the embedded module.

PR: #35129

Tests: bun bd test test/bundler/bundler_compile.test.ts -t "ChildProcessForkEmbedded|ClusterFork" (both fail on the released binary, pass with the fix).

CI: the only hard failures are test-net-connect-memleak.js and test-gc-http-client-connaborted.js, both GC-timing tests also red on main (builds 77601 / 77753) and unrelated to this diff; both pass locally against this branch's build. Reported for main-break triage.

@coderabbitai

coderabbitai Bot commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@robobun, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 8 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 1cd96301-7d91-47dc-bd00-2a5fdbcd5fab

📥 Commits

Reviewing files that changed from the base of the PR and between 47597ab and e249586.

📒 Files selected for processing (7)
  • src/bun_core/env_var.rs
  • src/js/node/child_process.ts
  • src/jsc/VirtualMachine.rs
  • src/jsc/web_worker.rs
  • src/resolver/standalone_module_graph.rs
  • src/runtime/cli/mod.rs
  • test/bundler/bundler_compile.test.ts

Comment @coderabbitai help to get the list of available commands.

@robobun

robobun commented Jul 22, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 10:38 AM PT - Jul 22nd, 2026

❌ @robobun, your commit e249586 has 3 failures in Build #77898 (All Failures):


🧪   To try this PR locally:

bunx bun-pr 35129

That installs a local version of the PR into your bun-35129 executable, so you can run:

bun-35129 --bun

Comment thread src/runtime/cli/mod.rs Resolved
Comment thread test/bundler/bundler_compile.test.ts Resolved
robobun and others added 2 commits July 22, 2026 13:21
…rk env var from process.env

The extension-remapping probe now lives as a provided method on the
resolver's StandaloneModuleGraph trait so new Worker(path) and
child_process.fork(path) inside a compiled binary use the same table.

BUN_INTERNAL_FORK_ENTRY is removed from the env loader map alongside
NODE_CHANNEL_FD so it never shows up in process.env. std::env::remove_var
alone does not touch the WTF-8 snapshot on Windows.

The compile/ChildProcessForkEmbedded test now reads stdout and the IPC
message from state inside the 'close' handler so the assertion does not
depend on message vs exit ordering.
Comment thread src/runtime/cli/mod.rs Resolved
…ONS tokens

append_options_env inserts those tokens at argv[1], so a spliced flag value
that happens to equal the fork module path would match first and shift the
child's process.argv. offset_for_passthrough is already the first OS-passed
arg slot.
Comment thread src/js/node/child_process.ts Resolved
Comment thread src/resolver/standalone_module_graph.rs Resolved
The merged env object in fork() now starts with __proto__: null like the
other option merges in this file.

resolve_entry_point_specifier keeps its original ./ and ../ gate around
the embedded-graph probe so new Worker('bare.ts') still falls through to
the resolver; only the extension table is shared.
Comment thread src/runtime/cli/mod.rs Resolved
…s the module-path slot directly

The value is now '<execArgv.length>:<modulePath>'. boot_standalone strips the
prefix and takes offset_for_passthrough + execArgv_len as the module-path argv
index, so there is no byte-equality scan left to collide with an execArgv
token or a user arg that happens to equal the module path.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All prior review feedback has been addressed and no new issues were found on e249586. Deferring to a human for the design sign-off: this introduces a new internal env-var protocol (BUN_INTERNAL_FORK_ENTRY=<execArgvLen>:<modulePath>) and rewires the boot_standalone entry-point selection, which is worth a maintainer's eyes.

What was reviewed:

  • The <count>:<path> encoding and offset_for_passthrough + exec_argv_len index math — traced through both the compile_exec_argv/BUN_OPTIONS splice branch and the no-splice branch; the value-scan is gone and .min(argv.len()) prevents overshoot.
  • resolve_embedded_entry vs. the removed web_worker.rs block — the .//../ gate is restored at the Worker call site so new Worker(bare) semantics are unchanged; the extension probe matches the old table byte-for-byte.
  • Env-var hygiene — scrubbed from the env-loader map in VirtualMachine.rs (covers the Windows WTF-8 snapshot) and remove_var for POSIX libc environ; the test asserts leaked=false.
  • Test now waits on 'close' and captures the IPC message into a variable, so the earlier ordering concern is resolved.
Extended reasoning...

Overview

This PR makes child_process.fork(modulePath) (and by extension cluster.fork()) work inside a bun build --compile binary. It touches seven files: a new BUN_INTERNAL_FORK_ENTRY env-var declaration in bun_core/env_var.rs; the fork() implementation in src/js/node/child_process.ts (sets the env var when spawning self); src/runtime/cli/mod.rs (boot_standalone reads/consumes the env var and picks the entry module + passthrough slice); src/jsc/VirtualMachine.rs (scrubs the var from process.env); a new shared resolve_embedded_entry default method on the StandaloneModuleGraph trait in src/resolver/; src/jsc/web_worker.rs refactored to call it; and two new itBundled tests in bundler_compile.test.ts.

Security risks

Low. The env var is internal, only honoured when the process is already a standalone executable, and only selects among modules already embedded in the binary (or resolves against cwd, which is what plain bun <file> does anyway). The __proto__: null on the merged env object was added per earlier review. No new external input surface.

Level of scrutiny

High. boot_standalone is the entry point for every compiled executable, and the change introduces a small internal protocol between parent and child processes. The Worker resolution refactor moves ~60 lines of load-bearing extension-remapping logic into a shared trait method. Over four review iterations the implementation has converged (Windows env snapshot, argv-index computation replacing a value scan, null-proto env merge, preserving the Worker ./ guard), and the current diff looks correct to me — but the design choice of env-var signaling (vs. e.g. an argv sentinel or a reserved flag) and the interaction with compile_exec_argv/BUN_OPTIONS splicing are the kind of thing a maintainer should ratify.

Other factors

All four of my prior inline threads are resolved by the author with targeted follow-up commits, and the multi-agent bug hunter found nothing on the latest revision. CI failures noted in the thread are pre-existing GC-timing tests also red on main. Two new tests cover both fork() and cluster.fork() and assert argv shape, IPC round-trip, and that the internal env var does not leak into process.env. Given the breadth (CLI boot path + Node-compat JS + resolver trait + Worker refactor) and that this is a new user-facing capability, I'm deferring rather than approving.

@robobun

robobun commented Sep 13, 2026

Copy link
Copy Markdown
Collaborator Author

Closing as part of a cleanup of stale pull requests. This PR has had no new commits since 2026-07-22, it conflicts with main, and its last CI run failed. This is not a judgment on the fix itself. If the problem still reproduces on a current build, reopen this PR after a rebase or open a new one against main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants