Skip to content

tests: more polled files are written non-atomically, so PID and JSON reads can see an empty file (two checks can pass vacuously) #3435

Closed

Description

@schlessera

Context

Found by the bug-class audit for #3432 (and #3401 before it). Many tests poll for a file a child writes, then read a value from it. The child writes it with writeFileSync or Bun.write, which create the file before writing it. A poll can therefore read the file empty or partial. A PID read as Number("") is 0, and PID 0 is special everywhere: /proc/0 never exists, process.kill(0, …) signals the caller's own process group, and process.kill(-0, …) does the same. #3433 (storage-inventory) and #3432 (test-invocation-lifetime) fixed two files. These instances remain on main at 3d3516a02.

None of these has been observed failing. They were found by reading the code. The fix shape is the one #3433 and #3432 use: write a temporary file, then rename it into place. Where a PID is read, also refuse anything but a positive integer.

Vacuous or hazardous (a check can pass without proving anything, or the test signals its own process group):

  1. tests/planning-observer.test.ts:127-129 polls for holding, which tests/helpers/planning-control-fixture.ts:25 writes with writeFileSync. Read empty, namespace is "", so living() matches no process and CANCELLED_NAMESPACE_HAS_NO_LIVING_DESCENDANTS (:141) passes vacuously.
  2. tests/ci-runner.test.ts:174-181: the child writes pid with Bun.write. The finally block runs process.kill(-Number(readFileSync(pid)), "SIGKILL"). Read empty, that is process.kill(-0), which sends SIGKILL to the test process's own group. Only try {} catch {} stands between it and the runner.

Fail-closed (a false red under load, not a false green):

  1. packages/ui-server/tests/storage-collection.test.ts:74-81: PID published with Bun.write. With PID 0, process.kill(0, 0) succeeds, so STORAGE_CANCEL_REAPS_CHILD fails falsely. This is the in-process form of test(core): storage-inventory 'CLI cancellation … leaves no stalled Git process' fails intermittently #3401.
  2. tests/integration-command.test.ts:124 (writer) and :209-211 (JSON.parse of held).
  3. tests/test-invocation-workers.test.ts:21 (writer) and :23-24 (JSON.parse of the sibling's file).
  4. packages/ui-server/tests/helpers/native-suite-timeout.ts:97 (writer) and :110-112 (JSON.parse of the checkpoint).
  5. packages/ui-server/tests/fixtures/inbox-dispatch-host.ts:67 (writer) and packages/ui-server/tests/integration/inbox-dispatch-recovery.test.ts:56-58 (JSON.parse of ready.json).
  6. scripts/native-proof/hosted-app-child.ts:287 with scripts/native-proof/hosted-app.test.ts:100-102, and scripts/native-proof/reference-integration-child.ts:159 with scripts/native-proof/reference-integration.native.ts:122-124 (ready.json).
  7. packages/ui-server/tests/fixtures/storage-notify-process.ts:12 (writer) and packages/ui-server/tests/storage-notify.test.ts:140-142 (exact-content check).
  8. packages/ui-sdk/tests/worker-turn-temp.test.ts:226,235-236 and tests/turn-temp-backends.test.ts:22,29-30 (exact-content check after an existence poll).
  9. packages/ui-server/tests/boot-probe-deadline.test.ts:370 (shell echo … > truncates before it writes) and :375-377.

Audited and already safe: packages/ui-sdk/tests/maintenance-worker-kill-child.ts:113-117 and packages/ui-server/tests/fixtures/inbox-unwind-worker.ts:29-33 (rename), tests/test-script.test.ts:158-159 (rename), scripts/native-proof/native-lease-child.ts:17 (rename), tests/workspace-lease.test.ts:40 (newline-terminated PID). tests/build-concurrency.test.ts:106-108 reads its PID file only during cleanup, where PID 0 skips the cleanup instead of signalling.

Scope

Publish every file listed above atomically (temporary file, then rename) in the child that writes it, or make the reader accept only a complete value (for example a newline-terminated positive PID, as tests/workspace-lease.test.ts does). Where a PID is read, refuse 0, negatives and unparsable text before it is used.

Out of scope

Acceptance criteria

  • Items 1 and 2 are fixed. For item 1, a forcing receipt (a widened create/write window) shows the base passing with a descendant left alive and the fix failing. For item 2, a receipt shows the cleanup refusing PID 0.
  • Items 3-11 are fixed, or each one left alone is listed in the PR with a reason.
  • The PR's audit re-runs the search and lists any new instance.

Verification

bun run test on each touched file, plus forcing receipts for items 1 and 2.

Contract impact

None. Test-only.

Refs #3432, #3401.

Activity

  1. added
    type: test
    area: ui-sdk
    area: ui-server
    area: ci
    priority: p2
    on Nov 3, 2026
  2. self-assigned this
    on Nov 3, 2026
  3. schlessera commented on Nov 3, 2026

    @schlessera
    OwnerAuthor

    Claiming for implementation (claim-3435-c8880f39). Branch: fix/3435-atomic-test-file-reads.

    Collisions (bun scripts/collisions.ts --paths over the cited files): path overlap with #3309 (tests/integration-command.test.ts) and #3386 (scripts/native-proof/hosted-app-child.ts, scripts/native-proof/hosted-app.test.ts); the remainder are closure-only. Builds on the pattern of #3439 and #3433, both still open.

  4. added
    agent-ready
    on Nov 3, 2026
  5. added this to the 0.45.0 milestone on Nov 3, 2026
  6. schlessera commented on Nov 3, 2026

    @schlessera
    OwnerAuthor

    Implementation is in #3444, a ready-to-confirm draft at head cbfb142, milestone 0.45.0. It fixes items 1-11 and one new instance found by the audit re-run (packages/core/tests/module-mcp-tools.test.ts). Forcing receipts, mutation receipts and loaded runs are in the PR. Not a proof-system change.

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

Metadata

Metadata

Assignees

Labels

agent-readyarea: ciarea: ui-sdkarea: ui-serverpriority: p2type: test

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions