Skip to content

node child process maxbuf support - #18293

Merged
Jarred-Sumner merged 19 commits into
mainfrom
pfg/child-process-4
Apr 4, 2025
Merged

Jarred-Sumner merged 19 commits into
mainfrom
pfg/child-process-4

Conversation

@pfgithub

@pfgithub pfgithub commented Mar 19, 2025 •

Copy link
Copy Markdown
Contributor

What does this PR do?

sync only

part of BUN-10684, Fixes BUN-12626, Fixes BUN-13251

TODO:

  • linux (may have to disable memfd?) (feature flag?)
  • windows
  • pass tests
  • non-sync
  • make sure non-sync isn't waiting up to a full tick
  • docs
  • maxBuf should limit the arraylist size (so running "yes" with limit 100 won't store 546173 bytes of output before the process is killed)

test failures:

  • test/js/node/test/parallel/test-child-process-fork-closed-channel-segfault.js

not tested:

  • this will slow down execFileSync when a max buffer is not specified because it no longer uses a memfd. It should not affect Bun.spawnSync/

sometimes channel won't be 'null' on close on linux musl

this is because sometimes 'process.on("close")' comes before 'process.on("disconnect")'

start
once disconnect; set channel <- common case
once close; set channel

start
once close; set channel <- rare case
once disconnect; set channel

this is probably supposed to always be called before close?

it reproduces easily in rr

@robobun

robobun commented Mar 19, 2025 •

Copy link
Copy Markdown
Collaborator
Updated 5:02 PM PT - Apr 3rd, 2025

✅ @pfgithub, your commit 605e0887cf643a214179b87616e912b52a703276 passed in Build #14445! 🎉


🧪   try this PR locally:

bunx bun-pr 18293

@pfgithub
pfgithub force-pushed the pfg/child-process-4 branch from f54bda6 to 8afd7a5 Compare March 21, 2025 00:17
@pfgithub
pfgithub force-pushed the pfg/child-process-4 branch from 06ed332 to 06380e2 Compare March 27, 2025 03:13
@Jarred-Sumner

Copy link
Copy Markdown
Collaborator

suspicious flaky test

63 |
64 |     worker.process.once(
65 |       'close',
66 |       common.mustCall(function() {
67 |         // Otherwise the crash on \`channel.fd\` access may happen
68 |         assert.strictEqual(worker.process.channel, null);
                    ^
AssertionError: Expected values to be strictly equal:
+ actual - expected
 
+ Control {
+   Symbol(kCapture): false,
+   _events: [Object: null prototype] {},
+   _eventsCount: 0,
+   _maxListeners: undefined
+ }
- null
 
      at innerFail (node:assert:26:32)
      at strictEqual (node:assert:149:14)
      at <anonymous> (/var/lib/buildkite-agent/builds/ip-172-31-26-206/bun/bun/test/js/node/test/parallel/test-child-process-fork-closed-channel-segfault.js:68:16)
      at emit (node:events:92:22)
      at #maybeClose (node:child_process:740:16)
      at #onDisconnect (node:child_process:716:21)
 
Bun v1.2.7-canary.1+6ccb8b867 (Linux x64 baseline)

@pfgithub
pfgithub marked this pull request as ready for review March 31, 2025 21:23
Comment thread src/js/node/child_process.ts Resolved
Comment thread src/js/node/child_process.ts Resolved
Comment thread src/js/node/child_process.ts Resolved
Jarred-Sumner pushed a commit that referenced this pull request Aug 2, 2026
…wnSync timeout (#36792)

Follow-up to #36782, which unquarantined `spawn-maxbuf.test.ts` and
exposed a latent boundary flake in a different subtest.

## Failure

```
240 |     expect(timeEnd - timeStart).toBeGreaterThan(100); // make sure it actually waits
error: expect(received).toBeGreaterThan(expected)
Expected: > 100
Received: 100
✗ timeout kills the process > Bun.spawnSync [101.64ms]
```

Seen on main [build 87859](https://buildkite.com/bun/bun/builds/87859)
(alpine 3.23 x64, retry passed) and [build
87890](https://buildkite.com/bun/bun/builds/87890) (debian 13 aarch64,
red on every retry). The assertion has been in the file since #18293; it
was hidden by the whole-file `[ FLAKY ]` quarantine until #36782 removed
it.

## Cause

The `spawnSync` timeout deadline is computed as `CLOCK_MONOTONIC +
100ms` at nanosecond precision (`js_bun_spawn_bindings.rs`:
`now.add_ms(timeout_ms)`), and the timeout fires when `CLOCK_MONOTONIC
>= deadline` (`SpawnSyncEventLoop::tick_with_timeout`). So by the time
`spawnSync` returns, at least 100ms of monotonic time has elapsed.

The test measures with `Date.now()`, which truncates the wall clock to
whole milliseconds. For a real elapsed interval of 100.x ms, `floor(end)
- floor(start)` can be exactly 100 (e.g. start at T+0.1ms →
`Date.now()=T`, end at T+100.2ms → `Date.now()=T+100`). The test
duration `[101.64ms]` in the CI output confirms the wait itself was not
short.

Local 500-iteration probe on release:

```
{"min":100,"hit100":7,"under100":0,"iterations":500}
```

The delta lands on exactly 100 in ~1.4% of runs and never goes below
100, so `>=` is the tight bound.

## Fix

Change `toBeGreaterThan(100)` to `toBeGreaterThanOrEqual(100)`. The
lower bound is kept because it is the only thing in this test that would
catch `timeout: 100` being misread as `timeout: 0` (the
`exitedDueToTimeout`/`signalCode` checks would still pass if the kill
fired immediately). Node's own `test-child-process-spawnsync-timeout.js`
only asserts the upper bound and relies on `error.code === 'ETIMEDOUT'`;
Bun's `exitedDueToTimeout` is equivalent, but the `>= 100` here is cheap
extra coverage.

The sibling `toBeGreaterThan(1000)` assertions in the `timeout Infinity`
block measure a child `sleep 1` plus `bunExe()` startup, which is always
comfortably above 1000ms, so they are left as-is.

## Verification

```
$ bun bd test test/js/bun/spawn/spawn-maxbuf.test.ts
 16 pass
 0 fail
```

Five consecutive full-file debug runs pass.

Test-only change; no runtime behaviour is touched.

<!-- robobun:evidence:begin -->

---

**no test proof** · iteration 0 · Platform-specific test-only change;
deferring to CI.

<!-- robobun:evidence:end -->
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.

3 participants