Skip to content

Fix a timing-dependent hang in the PTY launch race test (CI only) - #30132

Merged
Jinwoo-H merged 1 commit into
mainfrom
ci/pty-launch-race-parity-runtime-lane-hang
Oct 17, 2026
Merged

Jinwoo-H merged 1 commit into
mainfrom
ci/pty-launch-race-parity-runtime-lane-hang

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​19 $\color{#cf222e}{\Huge{\mathbf{−}}}$​6 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​13
Prod 0 0 0 0

ELI5

One unit test sometimes hung for 30 seconds on busy CI machines and then failed. It was waiting a fixed number of "ticks" for a second terminal start request to reach a checkpoint, but on the way there that request creates a real folder on disk. When the machine is busy, creating the folder takes longer than those ticks, the request misses the checkpoint, and the test waits forever. The test now skips the real disk write, so the wait is always long enough. Test-only change; nothing in the app changes.

What Changed

  • Before: runtime lane: a spawn arriving while a window spawn is starting the pane joins it in src/main/ipc/pty-launch-race-parity.test.ts could hang until the 30 second test timeout under CI load (seen on the tests node 24 1/5 shard in runs 39074131915 and 39073094268, on PRs unrelated to terminals). Vitest then hit an unrelated error while formatting the timeout stack.
  • After: the test passes deterministically, and if the race it guards is ever broken it fails in milliseconds with a clear assertion instead of hanging.

The mechanism:

  1. The test starts a window spawn, then a second (host side) spawn for the same pane, then turns the event loop 20 times (flushMacrotasks) before letting the first spawn finish. The second spawn is supposed to reach the "join the spawn already in progress" check within those turns.
  2. On the host path, before that check, prepareRuntimePtySpawn awaits ensureJcodeRuntimeDir (src/main/ipc/pty/runtime/spawn-preflight.ts:114), which calls the real mkdir from node:fs/promises (src/shared/jcode-runtime-dir.ts:58). That runs on Node's background I/O threads, so how many event loop turns it takes depends on machine load. The test's file system mock covers fs, not fs/promises.
  3. If the mkdir finishes after the 20 turns, the first spawn has already finished and released its claim, so the second spawn sees nothing to join and starts its own process. The provider mock returned a promise that never settles for every call, so that second start hung forever.

The fix, all in the test file:

  • Mock ensureJcodeRuntimeDir to return the same environment without touching disk. With that, every step before the join check is in-process and finishes within two turns, so 20 turns are always enough. It also stops this test file writing folders into the real temp directory.
  • The provider mock now gates only the first call; any later call rejects at once, so a missed join fails fast instead of hanging.

Why

  • Raising the timeout or the number of turns would only make the guess bigger. The cause is real disk I/O inside a step the test counts event loop turns over, so removing that I/O fixes it at the source.
  • Waiting on a signal from inside the join was the other option, but the host path exposes no observable point at the join check without adding test hooks to app code. Mocking the one I/O step keeps the change test-only.
  • The product behaviour is unchanged; this PR touches only the test. Whether a host-side spawn that arrives after the window spawn finished re-checks the pane's new owner is a separate question, logged for follow-up, and this test does not cover it.

Linked Issue

N/A (CI flake, internal).

Visual Proof

N/A: test-only change, no UI or behaviour change.

Testing

Machine: 18-core Mac, load average 22 to 28 before adding load. Load during each stress batch: 8 CPU-burning processes plus two stress loops running the same file at the same time. Each run: pnpm exec vitest run --maxWorkers=4 --config config/vitest.config.ts src/main/ipc/pty-launch-race-parity.test.ts.

  • Before, plain main (d9a6143): 0 failures in 50 runs. The natural flake did not reproduce on this machine.

  • Mechanism proof on main: instrumented the host path. Unloaded, the mkdir finishes on turn 2 and the join check runs on turn 2 of 20. Adding an artificial 50 ms delay before the mkdir makes the join check run after turn 20, finds no spawn to join, and the test hangs and fails at exactly 30000 ms, matching CI.

  • After: 0 failures in 50 runs under the same load. With the fix, removing flushMacrotasks entirely makes the test fail in 58 ms instead of hanging, confirming the fail-fast path.

  • pnpm tc, pnpm run check:code-quality:changed, pnpm run audit:anti-slop: pass.

  • Neighbouring tests (pty-launch-race*, pty-ipc*, src/main/ipc/pty/runtime, jcode-runtime-dir): 9 files, 67 tests pass.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

AI Disclosure

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Test-only. No effect on security, platforms, SSH, mobile, compatibility, or performance.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8b4803b8-2bdc-4440-80a7-47bf6685a1b7
📥 Commits

Reviewing files that changed from the base of the PR and between 27cfee6 and e6c38b6.

📒 Files selected for processing: 1
  • src/main/ipc/pty-launch-race-parity.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.

📝 Walkthrough

The test imports the runtime-directory module type and partially mocks the module. The mock preserves the actual exports and replaces ensureJcodeRuntimeDir with an async function that returns the environment built for the pane key. The spawn mock resolves its first call and rejects later calls with joiner_spawned_its_own_pty.

Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to e6c38

This CI-only change removes filesystem timing from the PTY race test while preserving its join check; it does not change runtime behavior, and no PR-specific issue blocks merging.

Pre-merge checks: Passed 5
Check name Status Explanation
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 1…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title clearly and concisely describes the main change: preventing a timing-dependent hang in the PTY launch race test during CI.
Description check Passed The description is complete and well scoped. It explains the failure, mechanism, fix, rationale, testing, and impact. It also covers the required visual-proof, checklist, and compatibility information…
Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 1 files.

Full details: Description check

Explanation

The description is complete and well scoped. It explains the failure, mechanism, fix, rationale, testing, and impact. It also covers the required visual-proof, checklist, and compatibility information for a test-only change.

✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Jinwoo-H
Jinwoo-H merged commit ce2d608 into main Oct 17, 2026
31 checks passed
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.

1 participant