Skip to content

bug: io.test.cjs fault-injection mocks swallow real fs.writeSync bytes, risking node:test IPC corruption #4306

Description

@trek-e

What's broken

tests/io.test.cjs's "bug #1008" tests install a process-wide fs.writeSync mock scoped only by file descriptor (if (fd !== 1) return orig(...)). On the "success" arms — the retry-after-EAGAIN/EINTR call, and the short-write simulation — the mock fabricates a byte count and returns it without ever calling the real orig(): the bytes are pushed into a local written array and never reach the real fd.

Node's node:test runner defaults to --test-isolation=process (Node >= 22): each test file runs as a child process, and the parent reads the child's own stdout to parse its TAP/result protocol. If the runner's own reporter write for an adjacent test happens to land on fd 1 while one of these mocks is installed, that write is silently swallowed instead of reaching the real pipe -- the parent then tries to parse a truncated/corrupted byte stream.

Observed symptom

CI run 33935441958 (full test (macos-latest, 24, shard 3/3), commit 0fca71eaae, reported by the maintainer as origin/next going red immediately after a merge):

failureType: 'uncaughtException'
error: 'Unable to deserialize cloned data.'
code: 'ERR_TEST_FAILURE'
stack:
  #processRawBuffer (node:internal/test_runner/runner:470:33)
  FileTest.parseMessage (node:internal/test_runner/runner:376:29)
  Socket.<anonymous> (node:internal/test_runner/runner:524:15)
  ...

That message is Node's generic "failed to v8.deserialize a corrupted/truncated buffer" error -- consistent with a byte stream that had bytes silently removed, not with a thrown exception propagating.

Why it fired now

The mechanism has been latent in this test file for a while (present in existing tests, not newly introduced by the triggering merge). The merge that immediately preceded the red run added tests/response-language-coverage.test.cjs (918 lines) -- the test-full job's own sharding comments (.github/workflows/test.yml, #3787) already document that adding a test file reshuffles which files land in which shard, changing timing. That reshuffle is the most plausible trigger for hitting the pre-existing race window this time; a re-run of the same shard/commit passed clean, consistent with a timing-dependent hit rather than a deterministic failure.

There's already a test in the same file guarding half of this risk ('regression: the fault-injection mock does not intercept writes to an unrelated fd') -- it proves fd 2 passes through untouched, but never checks whether the fd-1 interception itself can eat someone else's write on fd 1.

Fix

Every "success" arm of these mocks should forward the real bytes to orig() (or restore()) instead of fabricating a return value while discarding the write, so anything else sharing the fd during the mocked window still gets its bytes delivered for real. Fixing directly in a companion PR.

Activity

  1. added
    bugSomething isn't working
    on Sep 5, 2026
  2. github-actions commented on Sep 5, 2026

    @github-actions
    Contributor

    Closing automatically — this bug report does not include a valid GSD Version.

    A version is required to reproduce and triage bugs. Grab it with one of:

    npm list -g @opengsd/gsd-core
    npx @opengsd/gsd-core --version
    

    Then edit this issue to add the version (e.g. 1.18.0) and reopen it, or comment with the version and a maintainer will reopen it. If a version genuinely does not apply, a maintainer can add the version-exempt label.

  3. reopened this on Sep 5, 2026
  4. trek-e commented on Sep 5, 2026

    @trek-e
    CollaboratorAuthor

    Update: scope is wider than the original report

    The originally-fixed instance (tests/io.test.cjs's bug #1008 fault-injection mocks) is one occurrence of a defect class, not the only one. An isolated adversarial review of the initial fix found the identical unsafe pattern copied across the suite: a mocked fs.writeSync fabricates a return byte count on its "matching fd" arm without ever forwarding the call to the real fs.writeSync, so any write from elsewhere in the process (most importantly Node's own node:test runner IPC/reporter traffic) landing on that fd during the mocked window is silently swallowed. Some instances extend the same defect to a second fd (typically fd 2/stderr), fabricating-and-discarding it instead of writing it or capturing it.

    18 files, ~22 call sites carried this pattern:

    • tests/io.test.cjs (the originally reported file)
    • tests/state-rebuild-cli.test.cjs
    • tests/state.test.cjs (2 functions)
    • tests/core-utils.test.cjs
    • tests/phase-locator.test.cjs
    • tests/commit-docs-bypass.test.cjs (2 functions)
    • tests/config-get-default.test.cjs (2 functions)
    • tests/estimate-calibrate.test.cjs
    • tests/discuss-mode.test.cjs
    • tests/milestone-lock.test.cjs
    • tests/gap-checker.property.test.cjs
    • tests/codex-inherit-smoke.test.cjs
    • tests/worktree-base-ref.test.cjs
    • tests/commands.test.cjs
    • tests/agent-install-check.test.cjs
    • tests/init.test.cjs (2 functions)
    • tests/roadmap.test.cjs (2 functions)
    • tests/task-command-router-resolve-content.test.cjs (2 functions)

    Fix approach changed accordingly. Rather than patching each copy in place, tests/helpers.cjs now exports one shared, safe helper — captureFdSync(fd, fn) — that always calls the real fs.writeSync first (so nothing is ever swallowed) and only captures the bytes for the fd being observed. Every file above has been migrated to delegate to it, eliminating the copy-pasted defect at its source instead of leaving 18 independent copies of the same risk in the tree. One file (tests/worktree-base-ref.test.cjs) needed a narrower, in-place fix instead of a full migration, because that specific test tracks every fd a write touched (not just one), which the shared helper doesn't expose — it now forwards to the real fs.writeSync and returns its true byte count rather than migrating to captureFdSync.

    The PR for this issue is still pending final verification (fresh orthogonal review + a gsd-test run) over the full expanded diff before it opens.

  5. added 2 commits that reference this issue on Sep 5, 2026
  6. trek-e commented on Sep 5, 2026

    @trek-e
    CollaboratorAuthor

    Reopening: found a second, independent instance of the exact same defect class after #4308 merged. An unrelated PR (#4281) hit the identical `"Unable to deserialize cloned data"` failure on `tests/io.test.cjs` in CI, on a branch that already had #4308's fix merged in — meaning the fix was incomplete, not that this was a fresh occurrence of infra flakiness.

    Root cause: `tests/io.test.cjs` has a SECOND, separate pair of `fs.writeSync` mocks (in the `#3912 A6: error() stderr bytes are unchanged by this phase` describe block — a different block than the "bug #1008" one #4308 fixed) that fabricate a return byte count for fd 2 without ever forwarding the write to the real `fs.writeSync`. Same defect class, missed in the original sweep because the search that found the 18 affected files was scoped around the "bug #1008" describe block's known pattern and didn't catch this second, differently-named block in the same file.

    Fix: https://github.com/open-gsd/gsd-core/pull/new/fix/4306b-io-test-stderr-bytes-mock (opening PR now).

    Note: this specific fd-2 mock structurally cannot explain the fd-1-based corruption node:test's runner protocol depends on (confirmed via source-level research: the parent reads report frames off the child's stdout, fd 1, not fd 2) — so it doesn't fully explain why #4281 hit the corruption on a branch with #4308 already merged. Fixing it regardless, since it's the same defect class and a real latent risk on its own. Continuing to look for any other remaining unsafe fd-1 mock.

  7. reopened this on Sep 5, 2026
  8. added 2 commits that reference this issue on Sep 5, 2026
  9. added a commit that references this issue on Sep 7, 2026
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

    bugSomething isn't workingconfirmed-bugVerified reproducible bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions