Skip to content

Process.run runs on the Node host, on child_process's spawnSync - #1469

Closed
BaaRai wants to merge 1 commit into
bendlang:mainfrom
BaaRai:process-run-node-host
Closed

BaaRai wants to merge 1 commit into
bendlang:mainfrom
BaaRai:process-run-node-host

Conversation

@BaaRai

@BaaRai BaaRai commented Oct 10, 2026

Copy link
Copy Markdown

Fixes #1468.

Problem

Process.run on the Node host ended the run with ReferenceError: Worker is not defined before any answer: the effect's worker path needs Bun's global Worker and Bun.spawn, and a host without them has no way to run the child. Recent fixes verify the emitted standalone program on standalone Node too (#1446, #1450), so the gap was reachable from a practiced lane.

Change

bend2/effs/process_run.js: after the shared validation, a host without Bun takes a new process_run_host, on node:child_process.spawnSync, which is the semantics the C lane models (#1392): stdin fed, stdout and stderr read until they close (a descendant holding them extends the run), the exit code with signaled exits as 128 + the signal, a SIGKILL at the deadline, and the spawn error handed back as the errno.

The output cap needed care, since spawnSync bounds each stream and the C lane bounds the sum:

  • maxBuffer is max_output + 1 per stream, so a stream past it alone puts the sum past the combined cap, and the sum is checked after the read; the answer is EFBIG exactly when the C lane's combined cap says so. The interim buffer is at most twice the cap.
  • Node reports the cap as ENOBUFS (v22 here; the older code is ERR_CHILD_PROCESS_STDIO_MAXBUFFER, kept for older hosts), and a flood that a descendant keeps past the deadline may surface as ETIMEDOUT with the partial buffers attached, so any error whose buffered sum is past the cap answers EFBIG: the cap fired first either way.

The Bun worker path is unchanged behind typeof Bun === "undefined".

Verification

On Linux x86_64 (WSL Debian 12), main at 5e70819 plus this branch:

tests/io/process_run.bend, the permanent regression, emitted standalone (-o out.js) and run on every host. Before the fix Node ended the run at the first call; after:

Host Result
Bun 1.4.2 (bun out.js) 19/19 True
Node v22.14.0 (node out.js) 19/19 True
interpreted (bend tests/io/process_run.bend) 19/19 True
native C lane 19/19 True

The Node run covers every row: stdin feeding, literal argv, exit and stderr, the at/over/combined cap rows (EFBIG), timeout (ETIMEDOUT), a missing executable (ENOENT), invalid limit and timeout (EINVAL), a NUL argument, a signal exit (143), SIGPIPE default, an inherited pipe past the deadline, descendant output after the child exits, a runaway descendant past the cap (this row is what pinned the ENOBUFS/flood rule above), closed stdio and the largest timeout.

tests/io/process_run_parallel.bend answers True on the interpreted, JS and C lanes.

Repo gate: 52/54 with and without this change. The two FAILs are the capped files bend2/comp.ts (64070) and bend2/bendtt.lean (64192) under this host's ttok (pip), files this PR does not touch; the same two FAILs appear on unmodified main here, and the maintainers' counter reports 63,995 for comp.ts (#1250), so the difference is the counter, not the tree.

No new test file: the gate runs the JS lane under Bun, where this path is unreachable (Bun keeps the worker path); the Node evidence above stands in for it, as in #1450.

A host without Bun has no Worker and no Bun.spawn, so Process.run ended
the run there with a raw ReferenceError before any answer. The node path
runs on node:child_process.spawnSync, the semantics the C lane models:
stdin fed, stdout and stderr read until they close, the exit code
(signaled: 128 + the signal), a SIGKILL at the deadline, the spawn error
handed back, and the combined output cap answered as EFBIG. The Bun
worker path is unchanged.

@Lorenzobattistela Lorenzobattistela left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @BaaRai for tracking this down! On main I get the same ReferenceError: Worker is not defined under Node, and with this branch process_run.bend passes 19/19 on Node 20 and 23. The Bun path and gates are unchanged.

Comparing the Node answers with Bun and C on some extra cases, the spawnSync path gives different results in a few of them:

  • Child doesn't read all its stdin. With input over the pipe buffer, e.g. Process.run("/usr/bin/head", ["-c", "3"], <100 KB>, 128, 3000) or /usr/bin/true, Node answers Fail 32 (EPIPE) and drops the child's status and output. Bun and C answer Done 0 with "zzz".
  • Descendant holding stdin. sh -c "sleep 2 <&0 >/dev/null 2>&1 & exit 4" with ~300 KB of input and a 500 ms timeout gives Fail 60 at the deadline on Node. Bun and C answer Done 4 right away.
  • Combined cap. When stdout + stderr together pass max_output but neither passes it alone (printf abc; printf def >&2; sleep 1; touch f, cap 5), the answer is still EFBIG, but Node lets the child keep running until it exits or the deadline, so f gets created. Bun and C kill it as soon as the sum goes over.
  • Small things: spawnSync reports the cap as ENOBUFS (checked on Node 20 and 23), so the ERR_CHILD_PROCESS_STDIO_MAXBUFFER branch never runs; env: { ...process.env } is spawnSync's default already; and the comments are much longer than the rest of effs/.

Ideally Node would answer exactly like the Bun and C lanes, and this effect would keep a single implementation if possible. A second one doubles what has to stay in sync (process_run.js goes from ~800 to ~1,400 tokens).

One way to get there that I tried locally: build the worker from node:worker_threads (new Worker(code, { eval: true }) + parentPort) and have it use child_process.spawn, which both Bun and Node provide. Then the same worker serves both runtimes, and it matched Bun and C on all the cases above (process_run.js ~800 → ~870 tokens). It does change the Bun path from Bun.spawn to child_process.spawn, though, and that's the code from #1392, so @nicolas-abril, are you ok with that direction?

@Lorenzobattistela Lorenzobattistela self-assigned this Oct 11, 2026
@Lorenzobattistela

Copy link
Copy Markdown
Collaborator

Closed: stale

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Process.run ends the run on the Node host instead of running the program

2 participants