Skip to content

fix(harness): convert runGrader to async spawn — resolves #1237 same-process deadlock - #1250

Merged
bokelley merged 3 commits into
mainfrom
bokelley/grader-spawn-fix
May 2, 2026
Merged

bokelley merged 3 commits into
mainfrom
bokelley/grader-spawn-fix

Conversation

@bokelley

@bokelley bokelley commented May 2, 2026

Copy link
Copy Markdown
Contributor

Summary

PR #1241 closed the stdin-pipe stall but the grader still hung 120s on storyboards that exercise upstream calls. Root cause: the harness boots the mock upstream HTTP server in the same Node process, then called spawnSync for the grader. spawnSync blocks the event loop — while it runs, the in-process upstream can't respond. Agent calls upstream, upstream's HTTP handler never gets a tick, agent blocks on the response, grader blocks on the agent, full deadlock at the configured timeout.

This is the residual hang flagged in #1237 that #1241 didn't address.

Diagnosis

A standalone reproducer (upstream in a separate child process) ran identical spawnSync invocations to completion in ~1s across three stdio configurations (pipe/pipe, pipe/inherit, inherit/inherit). That ruled out:

The hang was specific to the harness's combination of (a) in-process upstream + (b) synchronous child wait. Same-process deadlock.

Fix

Convert runGrader to async: spawn + readable-stream chunk collection + Promise on 'close', with setTimeout → child.kill('SIGTERM') for the 120s cap. The parent event loop stays live, so the in-process upstream serves alongside the grader.

Call site (main()) gets await.

Verification

signal_marketplace verify-mode against a kept blind-fixture workspace:

log size wall traffic
before 22 lines 120s _lookup/operator: 1 only
after 632 lines (full grader JSON) 1.5s _lookup/operator: 2, /v2/cohorts: 2

overall_status: partial matches direct-grader invocation. The harness's traffic check still correctly flags POST /v2/activations: 0 (separate storyboard-cascade issue, adcontextprotocol/adcp#3796).

Refs

Test plan

  • Async runGrader compiles (strict tsc)
  • prettier format clean
  • Verify-mode on signal_marketplace returns full grader JSON in 1.5s (was 120s timeout)
  • Traffic counters reflect actual storyboard execution
  • CI

🤖 Generated with Claude Code

bokelley added 2 commits May 2, 2026 06:40
PR #1241 closed the stdin-pipe stall but the grader still hung 120s
on storyboards that exercise upstream calls. Root cause: the harness
boots the mock upstream HTTP server in the SAME Node process, then
called `spawnSync` for the grader. spawnSync blocks the event loop —
while it runs, the in-process upstream can't respond. Agent calls
upstream, upstream's HTTP handler never gets a tick, agent blocks on
the response, grader blocks on the agent, full deadlock at the
configured timeout.

Standalone reproducer (upstream in a separate process) ran identical
spawnSync invocations to completion in ~1s, which ruled out stdio
buffering, webhook-receiver port collision, and pipe-buffer
exhaustion. The hang is specific to the harness's same-process
upstream + sync child wait.

Fix: convert `runGrader` to async — `spawn` + readable streams +
Promise on `'close'`, with a SIGTERM-on-timeout via setTimeout. The
event loop stays live, the upstream serves alongside the grader, the
deadlock is gone. Also `await` the call site.

Verified on signal_marketplace verify-mode:
- Before: 22-line log, 120s timeout, traffic shows only 1 lookup hit.
- After:  632-line log (full grader JSON), 1.5s wall, traffic shows
  the storyboard's full upstream activity. `overall_status: partial`
  matches direct-grader invocation.

Resolves #1237 (the residual hang #1241 didn't catch).
Code review on PR #1250 (code-reviewer + nodejs-testing-expert
converged on this) flagged a real failure path the prior version
missed: if `spawn` fails to start the child (ENOENT, EACCES) Node
emits 'error', and without a listener the harness throws an
uncaught exception. Without also resolving the Promise on 'error',
a failed spawn could hang the awaiter.

Fix:
- Settle the await on whichever of 'error' or 'close' comes first;
  capture the spawn error into the diagnostic.
- New log line: `grader: spawn failed — ENOENT: ...` so a missing
  `node` binary surfaces clean instead of a stack trace.
- Append the spawn-error message to `raw` so the matrix log carries
  it.

Per DX expert review: rebuild the timeout-debug hint from the actual
`args.join(' ')` so the fallback command can't drift from the spawn
call. Also surface the `--mode build --keep` + `--mode verify` debug
loop, which is the operator's actual recovery path.

Verified spawn-error semantics with `spawn('nonexistent', [])` — the
'error' event fires with code ENOENT as expected.

The TS narrowing dance (closure-assigned `let` → `never` at read
site) routed through a wrapper object; comment explains why.
@bokelley

bokelley commented May 2, 2026

Copy link
Copy Markdown
Contributor Author

Expert review pass: 3 reviewers in parallel (code-reviewer, nodejs-testing-expert, dx-expert). Convergent finding: one blocker — missing 'error' listener on the child. Addressed in 9b8ebb60:

Fixed:

  • Wired 'error' (ENOENT/EACCES) listener; settle Promise on whichever of 'error' or 'close' fires first. Without this, a missing node binary would either crash the harness with an uncaught exception or hang the awaiter forever.
  • New diagnostic line on spawn failure: grader: spawn failed — ENOENT: ...
  • Spawn error message appended to raw so it lands in the matrix log via the existing stderr-dump.
  • Per DX expert: rebuild the timeout debug-hint from the actual args.join(' ') so the suggested command can't drift from the actual spawn call. Hint also points operators at the --mode build --keep + --mode verify recovery loop.

Deferred to follow-ups (non-blockers):

  • Regression test for in-process-upstream + child-process-grader (testing expert suggested test/scripts/run-grader.test.ts — server with setImmediate response forces a live event loop)
  • Extract runWithStdoutCapture(cmd, args, { timeoutMs }) helper (DX expert: this is the second runGrader bug in two PRs; the plumbing is begging to be isolated from grading logic)
  • SIGKILL escalation 5s after SIGTERM if the child ignores the signal (code-reviewer nit)

Verified spawn-error semantics: spawn('nonexistent', []) emits error with code === 'ENOENT' as expected. Typecheck + prettier clean.

@bokelley

bokelley commented May 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review pass. The 'error' listener fix in 9b8ebb60 looks like the right call — a missing binary silently hanging the awaiter would be rough to diagnose in CI. The deferred items (regression test, runWithStdoutCapture helper, SIGKILL escalation) are well-scoped follow-ups; the helper extraction in particular seems worth doing before the next grader bug surfaces.


Generated by Claude Code

@bokelley

bokelley commented May 2, 2026

Copy link
Copy Markdown
Contributor Author

Agreed — the 'error' listener is the right move to surface missing-binary failures quickly rather than letting the awaiter hang silently. The three deferred items are tracked in the PR body; the runWithStdoutCapture helper in particular will pay for itself the first time someone adds another grader-adjacent command. Ready for merge when CI clears.


Generated by Claude Code

@bokelley

bokelley commented May 2, 2026

Copy link
Copy Markdown
Contributor Author

Acknowledged — agree the current state looks good. Waiting on CI; will keep an eye on the run.


Triaged by Claude Code. Session: https://claude.ai/code/session_019o91h3KWyxbAgSFZvejewE


Generated by Claude Code

The Check-for-changeset CI job classified the harness change as
needing a release entry even though `scripts/manual-testing/` isn't
shipped in the npm package's `files` field. Add a patch changeset
documenting the fix so the gate passes; the published library is
untouched.

The changeset body explains the rationale so the next maintainer
isn't confused by a "fix" entry that doesn't change runtime behavior.
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