Skip to content

fix(sandbox): handle abort signal and early stream close in runCommand#135

Open
pbzona wants to merge 4 commits intovercel:mainfrom
pbzona:fix/abort-signal-zod-validation
Open

fix(sandbox): handle abort signal and early stream close in runCommand#135
pbzona wants to merge 4 commits intovercel:mainfrom
pbzona:fix/abort-signal-zod-validation

Conversation

@pbzona
Copy link
Copy Markdown

@pbzona pbzona commented Apr 7, 2026

Summary

When runCommand is called with wait: true, the API returns an ndjson stream with two JSON chunks (command started, command finished). Two problems with how this stream is consumed:

  1. Abort signal hangs forever. If an AbortSignal fires mid-stream (e.g. AbortSignal.timeout()), the HTTP body closes but the jsonlines transform stream stays open. The iterator.next() call waiting for the second chunk blocks indefinitely — it never resolves or rejects.

  2. Early stream close throws ZodError. If the stream closes after the first chunk but before the second (e.g. server drops connection), iterator.next() returns { done: true, value: undefined }. CommandFinishedResponse.parse(undefined) then throws a Zod validation error instead of a meaningful message.

Fix

  • Wire the abort signal to jsonlinesStream.destroy() so pending iterator.next() calls reject immediately instead of hanging
  • Check iterator.done before parsing to throw a StreamError instead of a ZodError
  • Clean up the abort listener when the stream is fully consumed

Test plan

  • throws abort error (not Zod error) when signal aborts before stream finishes — abort mid-stream rejects instead of hanging
  • throws StreamError when stream closes before finished chunk arrives — early close throws StreamError, not ZodError
  • rejects when signal is already aborted before stream starts — pre-aborted signal rejects immediately
  • Existing runCommand and full suite tests still pass

First commit shows the intentionally failing tests to illustrate the behavior. Open to suggestions on the specific error handling behavior, I just don't think it should be a Zod error in these cases. It's technically expected but could be more helpful.

Validates two bugs in the streaming runCommand (wait: true) path:
1. Abort signal hangs forever — the finished promise never settles
   because the abort isn't propagated to the jsonlines iterator
2. Abrupt stream close produces ZodError instead of a descriptive
   error when iterator.next() returns { done: true, value: undefined }
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 7, 2026

@pbzona is attempting to deploy a commit to the Vercel Team on Vercel.

A member of the Team first needs to authorize it.

When runCommand is called with wait: true, the API returns an ndjson
stream with two chunks. If the abort signal fires mid-stream, the
jsonlines iterator hangs forever because the abort isn't propagated.
If the stream closes before the second chunk, the iterator returns
undefined which causes a confusing Zod validation error.

Wire the abort signal to destroy the jsonlines stream so pending
iterator.next() calls reject immediately. Guard against early stream
close by checking iterator.done before parsing. Clean up the abort
listener when the stream is fully consumed to avoid retaining
references on long-lived signals.
@pbzona pbzona force-pushed the fix/abort-signal-zod-validation branch from af6f156 to b04c23f Compare April 7, 2026 02:26
@pbzona pbzona marked this pull request as ready for review April 7, 2026 02:45
@pbzona pbzona force-pushed the fix/abort-signal-zod-validation branch from 2888ae4 to cbbb480 Compare April 7, 2026 13:02
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.

2 participants