Skip to content

bun test --isolate (Linux): intermittent unhandled EEXIST: file already exists, epoll_ctl while initializing process.stderr fails test files with no named tests #37968

Closed

Description

@7nohe

What version of Bun is running?

1.3.14 (0d9b296a), including the oven/bun:1.3.14 Docker image

What platform is your computer?

Linux only. Observed on GitHub Actions ubuntu-latest (x64) and reproduced in Docker linux/arm64 (Apple Silicon host). Not reproduced on native macOS — possibly because the failing syscall is epoll_ctl, which doesn't exist there, but that attribution is our guess.

What steps can reproduce the bug?

We hit this as an intermittent CI flake in a ~250-file bun test --isolate suite, and could reproduce it 8 out of 8 runs in a CPU-constrained container. We were unable to distill a standalone minimal repro (several targeted attempts — floods of stderr writes, node:assert imports across many isolate contexts, subprocess churn — never triggered it), so the recipe below uses the real suite at a pinned commit:

git clone https://github.com/gurenjs/guren && cd guren
git checkout --detach 9af07e910c297d9b4f6e096faee88def75ea55a7
docker run --rm --cpus=2 -v "$PWD":/src oven/bun:1.3.14 bash -c '
  cp -r /src /work && cd /work
  export APP_KEY=base64:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
  bun install --frozen-lockfile && bun run build && bun run build:routes
  # stderr must be a pipe (as under a CI runner); redirecting it straight to a
  # regular file made the failure vanish in every run we tried
  bun run test:bun > >(tee /tmp/out.log >/dev/null) 2> >(tee /tmp/err.log >/dev/null)
  echo "test:bun exit: $?"
  grep -c "EEXIST" /tmp/err.log
'

bun run test:bun expands (at that commit) to a single invocation of bun test --isolate --preload scripts/test-cwd-guard.ts <one path per workspace package> — the expanded package list is printed on the [test] line at startup.

Real-world occurrences on GitHub Actions, all on 2026-08-12 and none related to the commit under test (each passed on rerun or on adjacent commits): job 94121054100 (a docs-only change), job 94139795319, job 94143320406.

What is the expected behavior?

Touching process.stderr for the first time inside an isolate context — including implicitly, via internal:util/colors when node:assert loads — should give back a working stream, and a test file whose module graph triggers that touch should load and run normally. If stream creation does fail, the failure should be observable/catchable from JS, and the summary should say which files died.

What do you see instead?

Intermittently, at a test-file boundary, an unhandled EEXIST from epoll_ctl while process.stderr is being created:

# Unhandled error between tests
error: EEXIST: file already exists, epoll_ctl
      at new WriteStream (internal:fs/streams:244:58)
      at refresh (internal:util/colors:18:39)
      at internal:util/colors (internal:util/colors:24:16)
      at internal:assert/assertion_error (internal:assert/assertion_error:2:187)
      at loadAssertionError (node:assert:28:96)
      at get (node:assert:70:30)
      at node:assert (node:assert:588:29)
      at <anonymous> (node_modules/redis-errors/lib/modern.js:3:7)
      at <anonymous> (node_modules/redis-errors/index.js:3:88)
      at <anonymous> (node_modules/ioredis/built/cluster/index.js:5:7)
      at <anonymous> (node_modules/ioredis/built/Redis.js:6:7)
      at <anonymous> (node_modules/ioredis/built/index.js:4:18)

What we can see from the JS side: under --isolate, every test file gets a fresh globals context and process.stderr is re-created lazily per context (verified: a property tagged onto process.stderr in one test file is gone in the next). Its constructor takes the fast path at internal:fs/streams:244, this[kWriteStreamFastPath] = fd ? Bun.file(fd).writer() : ..., and that construction is what dies. In our suite the first process.stderr touch in a context usually happens mid-module-load, because the first file whose import graph reaches ioredis evaluates ioredis → redis-errors → node:assert → internal:util/colors, whose refresh() calls shouldColorize(process.stderr).

Knock-on effects, all observed in the same runs:

  1. The CJS module that was loading (ioredis) is left un-cached, so a later test file that imports it re-evaluates it and can crash the same way — single runs produced 4–6 of these reports, sometimes two in a row while loading the same test file's import graph.
  2. After a failed creation, process.stderr can come back undefined within that context, so debug's useColors() then dies with TypeError: undefined is not an object (evaluating 'process.stderr.fd') on the same load.
  3. The test file whose load was interrupted reports error: Cannot call describe() after the test run has completed for each of its describe() calls and is counted as failed.

Additional information

The failure is not observable from JS through the channels we tried. A try { process.stderr } catch {} around the first access never catches (the runner's unhandled-error report carries the construction stack, but no catch block ever sees it), and an 'error' listener attached synchronously to the returned stream never fires. And a preload cannot defuse it pre-emptively either: reading the property, Object.defineProperty(process, 'stderr', { value: replacement }), and delete process.stderr each reify the lazy property first — the native getter runs and constructs the stream as a side effect, which is the very operation that crashes. Each of these four preload variants was tried against the reproducing container and still failed, with the unhandled-error stack pointing at the preload's own line.

The workaround we verified: give the bun test process regular files instead of pipes for stdout/stderr and pump them back from a parent process. With that change the same container setup went 20/20 clean (vs. 8/8 failing before): gurenjs/guren#392

Possibly related (different symptom, same neighborhood — stdio stream creation failing on the event-loop registration syscall): #24158 (tty.WriteStream → EINVAL: invalid argument, kqueue on macOS).

Separate, smaller issue that makes this hard to triage from CI logs: when files die between tests like this, the summary prints N tests failed: but lists nothing under it — no (fail) line exists anywhere in the output. Naming the affected files there would help independently of the epoll bug. Happy to file that separately if preferred.

Activity

  1. robobun commented on Aug 13, 2026

    @robobun
    Collaborator

    ✅ Reproduced: under --isolate every test file re-creates process.stdout/process.stderr over a fresh dup of the stdio fd plus an epoll registration, and the global swap never closed them. The EEXIST is a stale leaked registration colliding with a reused fd number over the same pipe.
    Fix in progress → #38008

  2. Parikalp-Bhardwaj commented on Aug 13, 2026

    @Parikalp-Bhardwaj

    I traced this a bit further on current main and found a possible low-level cause in src/io/posix_event_loop.rs.

    On Linux, FilePoll::register_with_fd_impl() uses EPOLL_CTL_MOD when the poll is already registered or has NeedsRearm; otherwise it uses EPOLL_CTL_ADD.

    However, in unregister_with_fd_impl(), when NeedsRearm is set and force_unregister == false, the function returns without issuing EPOLL_CTL_DEL, while clearing the poll registration flags.

    That looks like it could leave the userspace state and kernel epoll state out of sync:

    FilePoll state: fd is not registered
    Kernel epoll:   fd is still registered
    

    A later registration of the same fd would then use EPOLL_CTL_ADD, which could explain the EEXIST error reported here.

    I also traced the stderr path roughly as:

    process.stderr
      -> Bun.file(fd).writer()
      -> FileSink
      -> StreamingWriter
      -> writable FilePoll
      -> epoll_ctl()
    

    I haven't confirmed yet that this is the exact teardown path hit by the reproducer, so I'm treating this as a root-cause hypothesis rather than a confirmed fix.

    Would the intended invariant on Linux/Android be that a FilePoll being torn down must always remove its epoll registration, even when NeedsRearm is set?

    If this direction looks correct, I'd be happy to add a focused regression test around the NeedsRearm -> unregister -> re-register path and work on a small fix.

  3. added a commit that references this issue on Aug 18, 2026
    33cba63
  4. juliopolycarpo commented on Aug 19, 2026

    @juliopolycarpo

    Independent confirmation from another codebase, plus one data point that contradicts the workaround in the issue body.

    Bun 1.3.14, a 301-file / 3423-test suite, reached through --parallel=N rather than an explicit --isolate (--parallel implies it). Same new WriteStream (internal:fs/streams:244:58) frame, same Cannot call describe()/beforeEach()/afterEach() after the test run has completed on the file that was loading, same "a few failures plus a drop in the total" summary shape.

    Host Invocation Runs affected
    GitHub ubuntu-latest, 4 vCPU, 6.17.0-1022-azure --parallel=4 2 of 36
    WSL2 6.18.33.2, taskset -c 0-3 --parallel=4 4 of 10
    WSL2 6.18.33.2, taskset -c 0-3 --parallel=8, stdout+stderr to a regular file 4 of 12
    WSL2 6.18.33.2, taskset -c 0-3 --parallel=8, stdout+stderr into a pipe 2 of 8

    Regular files did not make it vanish here — they made it likelier (4/12 vs 2/8). So the files-instead-of-pipes workaround looks host- or suite-dependent rather than reliable; worth a caveat for anyone adopting it off this thread.

    A second entry path into the same crash, which supports "any first stdio touch in a fresh context" over anything specific to node:assert: our stack came in through google-logging-utils, which does __importStar(require("process")) at module scope. That copies every own property off the process module and so reifies the lazy stdin/stdout/stderr getters eagerly. It is reached transitively via @google/genai → google-auth-library. Removing that package from the module graph removes those frames and the aborts continue, with a bare new WriteStream stack — so it is a messenger, not a cause.

    Reduction attempts that did not reproduce it, complementing the ones already listed: 200 files reading process.stdout/process.stderr at module scope; 200 files doing the __importStar property copy above; 120 files importing @google/genai; 300 trivial files under a heavy real preload (config, DI registration, in-memory SQLite, migrations). All at --parallel=8. The trigger seems to need the real suite's per-file work.

    One runner detail that may matter to the fd/isolate bookkeeping: under --parallel=N the main process also runs test files alongside the N-1 children, and files within any one process run strictly sequentially.

  5. juliopolycarpo commented on Aug 19, 2026

    @juliopolycarpo

    Following up on my comment above with a more controlled comparison, because the obvious result is misleading.
    Same 301-file / 3423-test suite, now in an ubuntu:24.04 container, each run pinned to an exclusive 4-core cpuset, with stdout/stderr on pipes. 234 runs across four Bun builds.

    I initially compared 1.3.14, current canary, and #38008, but that does not isolate the patch: canary is 2022 commits past the 1.3.14 tag and includes the Rust rewrite. I therefore added #38008's merge base, 23b18ddbc, so the relevant controlled comparison is:

    23b18ddbc → fd33b876d

    Canary no longer hits the abort, but still leaks

    Reproduced #38008's regression-test idea against each build: 8 isolate files, each counting how many open fds point to the same pipe as fd 2.

    build duplicated pipe fds, files 1→8
    1.3.14+0d9b296af 4 6 8 10 12 14 16 18
    1.4.0-canary.1+23b18ddbc (PR base) 4 6 8 10 12 14 16 18
    1.4.0-canary.1+32e87032b (canary) 4 6 8 10 12 14 16 18
    1.4.0-canary.1+fd33b876d (#38008) 4 4 4 4 4 4 4 4

    So the per-isolate stdout/stderr leak is unchanged on current canary. Only #38008 removes it.

    The suite-level result looks very different:

    build unit runs at ≥4 workers epoll_ctl aborts
    1.3.14 38 12 (32%)
    PR base 23b18ddbc 24 0
    canary 32e87032b 38 0
    #38008 fd33b876d 62 0

    Taken alone, that table suggests newer Bun builds fixed the issue. The fd counts show otherwise: the collision stopped being reachable in this workload, while its precondition remained unchanged.
    That seems worth calling out because a green run on current canary can look like a fix even though the underlying isolate stdio leak is still present.
    It is also independent confirmation that #38008 removes the leak it targets.

    The original failure signature is unchanged when it does occur:

    error: EEXIST: file already exists, epoll_ctl
          at new WriteStream (internal:fs/streams:244:58)
          at <anonymous> (node_modules/google-logging-utils/build/src/logging-utils.js:17:23)
          ...
     3321 pass
       11 fail
    Ran 3332 tests across 301 files.   # 91 of 3423 never ran
    

    Small follow-up on the regular-file workaround from my previous comment: it still does not improve this workload. At --parallel=8, 2/4 runs were affected with stdout/stderr redirected to regular files versus 6/12 with pipes. I would treat that workaround as host/workload-specific rather than general.

    Separately, I observed four near-end-of-suite worker hangs on the unfixed builds and none in 85 completed #38008 runs. The sample is too small to establish a relationship, and the symptom is distinct from this issue, so I am keeping that out of this report unless I can reduce it further.

    Environment: Ubuntu 24.04 userland, kernel 6.18.33.2-microsoft-standard-WSL2, 4-core cpuset per container. I would trust the build-to-build comparison here more than the absolute failure rates. --coverage was also tested as a variable and did not materially change the abort rate.

    Canary stops reproducing the symptom, but does not fix the underlying cause; #38008 does.

  6. added a commit that references this issue on Aug 25, 2026
    b09ec89
  7. added a commit that references this issue on Sep 4, 2026
    0445be9
  8. robobun commented on Sep 18, 2026

    @robobun
    Collaborator

    The way the registration goes stale is now traced, at least for Bun's own CI.

    A process.stderr sink from an earlier isolated file is released by the GC while Bun.spawnSync runs. During that call the VM's current event loop is the isolated spawnSync loop. So the sink's EPOLL_CTL_DEL goes to the wrong epoll (ENOENT), and the entry stays in the main epoll after the dup is closed. The next dup of the same stdio fd that gets that number fails with EEXIST. In 147 runs of one CI test bucket, 118 of 118 stale entries came from this. Trace: #40078 (comment).

    #40078 fixes the wrong-epoll release. #38008 ends the per-file sinks before they can reach that window. If a suite that hits this error calls spawnSync or execSync in the test process (directly or in a dependency), the same source is likely there.

  9. added a commit that references this issue on Oct 5, 2026
    13a98b0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions