Skip to content

fix(relay): a failed Exec must not return a gRPC OK status (#295) - #309

Merged
pdettori merged 2 commits into
rossoctl:mainfrom
pdettori:fix/295-relay-exec-error-status
Sep 21, 2026
Merged

pdettori merged 2 commits into
rossoctl:mainfrom
pdettori:fix/295-relay-exec-error-status

Conversation

@pdettori

Copy link
Copy Markdown
Member

Closes #295.

The defect

main.ts's exec handler ended the stream with call.end() — a gRPC OK — even when the
worker had reported the Exec as failed:

for await (const ev of relay.routeExec(req.sandboxId, e)) {
  call.write(ev);          // <- the ExecEvent.error goes out here
}
call.end();                 // <- ...and then we report success

routeExec yields an ExecEvent.error and then returns normally (relay.ts sets
done = true on ev.error), so the for await falls out and reaches call.end(). The
surrounding catch only ever fired for a thrown error, never an in-stream one.

A client that reads the terminal status therefore counted a failed Exec as a success: it
entered throughput, entered the distribution p95 is taken over, and never reached
execErrorsByCause. e11-density.sh's own exec_error_note_for() documents this on the
grpcurl path, and it is why the recent throughput runs had to use SH_E11_EXEC_CLIENT=go.

The fix

Terminate with INTERNAL, carrying the worker's message as the status details.

  • INTERNAL, not grpc-js's default UNKNOWN for a bare Error: ExecEvent.error means the
    exec machinery failed server-side. A command that merely exited non-zero is a different thing
    and arrives as ExecEvent.end{exitCode}.
  • The error event is still written before the status, so clients reading the in-stream error
    are unaffected. Both do: grpc-relay-transport.ts:166 rejects on ev.error, and the Go
    exec-driver tracks it as a failure. The TS transport's finish() is idempotent, so the added
    stream error is dropped as a late frame rather than double-settling.
  • A client cancel is excluded. It also arrives as an ExecEvent.error — the worker
    acknowledging our own abort — which is not a server-side failure. The pre-existing
    cancellation test passes unchanged, and that is what pins the distinction.
  • Presence is tracked as a flag, not by testing the message text. An ExecEvent.error with
    an empty message is still a failing Exec; classifying on the text would silently re-admit it
    as a success. The Go exec-driver's sawErr note records the same lesson.

Verification

check result
sandbox-relay suite 19 passed / 4 files, exit 0 (18 before; one added)
k8s-sandbox suite (the TS client of this stream) 152 passed, 11 skipped, exit 0
tsc --noEmit exit 0
make lint exit 0

Red-green was verified in both directions: the new test failed with expected undefined to be an instance of Error against the old handler, and the status-code assertion was separately proven to
fail (expected undefined to be 13) by temporarily stripping err.code.

Plan Step 4's check: no driver depends on the OK-on-error behaviour — e11-density.sh
documents it as a known defect rather than relying on it.

Deliberately not in this PR

e11-density.sh's exec_error_note_for() now describes a defect that no longer exists on the
grpcurl path (both branches go stale). Re-wording it means asserting how that path records status
after this change, which wants a metal run to confirm, and the file is contended by the
#302 / rossoctl/moca-experiments#8 / #305 work. Follow-up required — whoever picks up those issues should expect to
touch this note.

🤖 Generated with Claude Code

)

The worker reports a failed Exec as an in-stream ExecEvent.error and then
stops. routeExec yields that event and returns NORMALLY (relay.ts sets
done on ev.error), so the exec handler's `for await` fell out and reached
call.end() -- a gRPC OK. Its catch only ever fired for a *thrown* error,
never an in-stream one. A client that reads the terminal status therefore
counted a FAILED Exec as a success: it entered `throughput`, entered the
distribution `p95` is taken over, and never reached `execErrorsByCause`.
That inflates exactly the throughput figures the E11 work is trying to
move, which is why the recent runs had to use SH_E11_EXEC_CLIENT=go.

Terminate the stream with INTERNAL instead, carrying the worker's message
as the status details. INTERNAL rather than grpc-js's default UNKNOWN for
a bare Error: ExecEvent.error means the exec machinery failed server-side,
whereas a command that merely exited non-zero arrives as
ExecEvent.end{exitCode}. The error event is still written before the
status, so clients that read the in-stream error are unaffected -- both
do (grpc-relay-transport.ts rejects on ev.error, and the Go exec-driver
tracks it as a failure), and the TS transport's finish() is idempotent, so
the added stream error is dropped as a late frame rather than
double-settling.

A client cancel also arrives as an ExecEvent.error -- the worker
acknowledging our own abort -- and is not a server-side failure, so it is
excluded and still drains cleanly. The pre-existing cancellation test
passes unchanged, which is what pins that distinction.

Presence of the error is tracked as a flag, not by testing the message
text: an ExecEvent.error with an EMPTY message is still a failing Exec,
and classifying on the text alone would silently re-admit it as a success
(the Go exec-driver's sawErr note records the same lesson).

Not included: e11-density.sh's exec_error_note_for() now describes a
defect that no longer exists on the grpcurl path. Re-wording it means
asserting how that path records status after this change, which wants a
metal run to confirm, and the file is contended by the rossoctl#302/#306/rossoctl#305
work. Left for a follow-up.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>

@mrsabath mrsabath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tightly scoped fix for a real and well-diagnosed defect. One nit inline, no must-fix.

I verified the central claims rather than taking them from the description:

  • The root cause is exactly as described. relay.ts:125 is if (ev.end || ev.error) done = true, and routeExec's loop does if (ev.end || ev.error) return — so the generator returns normally after an error event, the for await falls out, and the old call.end() reported OK. The pre-existing catch genuinely could not fire, since nothing throws.
  • call.destroy(err) is the established pattern in this handler, not a new mechanism: the missing-exec-field guard (main.ts:76) and the catch block (main.ts:113) both already use it. The early return still runs finally, so the cancelled listener is removed on every path.
  • The cancel exclusion is genuinely pinned by the pre-existing test. That test emits cancelled, then feeds {error: {reqId: 1, message: 'aborted'}}, and asserts both call.ended === true and call.destroyed === undefined. Without the !cancelled guard it would fail — so "passes unchanged" is a meaningful claim here, not a vacuous one.
  • No race can bypass the guard in the cancel direction. The worker only emits the acknowledging error frame in response to the abort frame that routeAbort writes from inside onCancelled, so cancelled = true is always set first. The reverse ordering (real failure, then client cancel) does suppress the INTERNAL status, but the client has already cancelled and will not read the terminal status, so nothing is lost.
  • The flag-not-text decision is right and the reasoning generalises: ev.error.message ?? '' means an error event with an empty message still sets sawExecError, so it cannot be silently re-admitted as a success.
  • INTERNAL over UNKNOWN is the correct call — ExecEvent.error is a server-side machinery failure, and a non-zero command exit arrives separately as ExecEvent.end{exitCode}, so the two are not conflated.
  • The error event is still written before the status, so in-stream readers are unaffected — consistent with the note about finish() being idempotent in the TS transport.

The deferred e11-density.sh note is the right call given that file is contended by #302/#306/#305, and flagging it as follow-up rather than leaving it silent is the correct handling.

Areas reviewed: TypeScript (gRPC server handler, error semantics), tests, security (no secrets, no new deps)
Commits: 1, signed off ✓
CI status: all 13 checks passing (CodeQL, Trivy, lint, shellcheck, proto, microvm-gates, check)

Verdict: APPROVE — 1 nit, no must-fix.

function execStreamError(message: string): Error {
const err = new Error(message) as Error & { code: number; details: string };
err.code = status.INTERNAL;
err.details = message;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit — err.details is set but never asserted.

This is the field that actually carries the cause to the client, and it is the one part of execStreamError no test touches. The new test asserts .message and .code; the PR body reports proving the code assertion has teeth by temporarily stripping err.code (expected undefined to be 13), but details got no equivalent treatment.

It matters slightly more than a typical unasserted field because message and details are set from the same variable here, so a test reading .message passes whether or not details survives. Delete this line and the suite stays green — yet a client reading e.details (the conventional grpc-js accessor for status details) would get undefined. One more assertion closes it:

expect((call.destroyed as { details?: string } | undefined)?.details).toBe('exec failed: boom');

Not blocking: grpc-js populates the wire status from code/message on a destroyed call regardless, so the cause does reach the client today.

…ream

Follow-up on this branch's own fix. `call.destroy(err)` does not send a gRPC
status, so the exec handler replaced rossoctl#295's wrong-OK with something worse: no
terminal status at all.

On @grpc/grpc-js 1.14.4 a ServerWritableStream sends its status from `_final`
(which calls `call.sendStatus`), and Node's Writable never reaches `_final` once
`destroyed` is set -- `end()` short-circuits with ERR_STREAM_DESTROYED. Verified
against a real bound server and a real client: with `destroy` the client sat 12s
with no status, no `end`, no `close` and no `error`; with `emit('error')` it got
INTERNAL (13) carrying the worker's message as details. Same result whether or
not an event had already been written.

A hang is strictly worse than the wrong-OK it replaced, because an OK at least
lets a caller finish its rung.

The unit test could not catch this: its fake call object records `destroy(err)`
and models none of grpc-js's semantics, so it reported success for a handler
that in production answers nothing. The status is produced by the transport, not
by the handler, so it is now pinned over a real transport.

- Route every non-OK termination through `failExecStream`, which emits 'error' --
  the listener grpc-js registers in the stream's constructor is what converts the
  error and ends the stream, and so the only thing that puts a code on the wire.
- Fix the two PRE-EXISTING `destroy` paths too, which hang today. One is an
  ordinary production state: `routeExec` throws `no live worker for sandbox` when
  the worker died or has not attached yet, and that reached the handler's catch.
  A malformed request with no exec field now reports INVALID_ARGUMENT, as it is
  the caller's fault; codes for arbitrary internal throws are left to grpc-js
  rather than classified here, which is a relay-semantics decision of its own.
- New `main-exec-status.transport.test.ts` binds a real server and drives a real
  client over all three paths. It fails on the destroyed-stream code by timing
  out, not by reading a wrong code, which is the symptom being fixed.
- `fakeExecCall` now registers the 'error' listener grpc-js's own constructor
  registers, and keeps `destroy` only so a regression back to it stays visible.

Verified: sandbox-relay 22 passed / 5 files exit 0; k8s-sandbox 152 passed,
11 skipped, exit 0 (its client settles on the in-stream error first and `finish`
is idempotent, so the added status error is dropped as a late frame); make
typecheck exit 0; make lint exit 0.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
@pdettori

Copy link
Copy Markdown
Member Author

Review: blocking defect found, fixed on the branch (9e67162)

The fix as originally written did not work — and failed in a worse way than the bug it
targeted. call.destroy(err) does not send a gRPC status.

On @grpc/grpc-js 1.14.4, ServerWritableStream sends its status from _final:

_final(callback) { callback(null); this.call.sendStatus({...this.pendingStatus, ...}); }

and a non-OK pendingStatus is set only by the listener grpc-js registers in the stream's
own constructor:

this.on('error', err => { this.pendingStatus = serverErrorToStatus(err); this.end(); });

Node's Writable never reaches _final once destroyed is set — end() short-circuits
with ERR_STREAM_DESTROYED — so destroying sends no trailers at all.

Measured against a real server and a real client

handler termination client's terminal status
call.end() (the #295 bug) OK (0)
call.destroy(err) (this PR, as filed) nothing — no status, no end, no close, no error after 12s
call.emit('error', err) INTERNAL (13), details exec failed: boom

Identical with and without a prior call.write(). A hang is strictly worse than the
wrong-OK
: an OK at least lets a driver finish its rung, where this would stall it.

Why the test passed anyway

fakeExecCall.destroy is (err) => (c.destroyed = err). It records the call and models
none of grpc-js's semantics, so it reported success for a handler that in production
answers nothing. The terminal status is produced by the transport, not the handler, so
no fake can pin it.

What changed

  • All non-OK terminations go through failExecStream, which emits 'error'.
  • Two pre-existing destroy paths hang today and are fixed too. One is an ordinary
    production state: routeExec throws no live worker for sandbox when the worker died
    or has not attached yet, and that lands in the handler's catch. A missing exec field
    now reports INVALID_ARGUMENT; codes for arbitrary internal throws are left to grpc-js
    rather than classified here (that is a relay-semantics decision of its own).
  • New main-exec-status.transport.test.ts binds a real server and drives a real client
    over all three paths. Against the destroyed-stream code it fails by timing out, which
    is the symptom itself rather than a wrong code.
  • fakeExecCall now registers the 'error' listener grpc-js's constructor registers, and
    keeps destroy only so a regression back to it stays visible here.

Verification

check result
sandbox-relay suite 22 passed / 5 files, exit 0 (was 19/4)
k8s-sandbox suite 152 passed, 11 skipped, exit 0
make typecheck exit 0
make lint exit 0

RED→GREEN on the new test: 3 failed by 8s timeout before, 3 passed in 186 ms after.

The k8s-sandbox client is unaffected: it settles on the in-stream ev.error first and
finish() is idempotent, so the added status error is dropped as a late frame. grpc-relay-transport.ts:169
already has an 'error' listener, so the now-real client error is not an unhandled event.

Still deferred

e11-density.sh's exec_error_note_for() now describes a defect that no longer exists on
either branch — it is recorded into every rung record as a trustworthiness disclosure, so it
should be reworded rather than left asserting the relay returns OK on a failed Exec. Worth a
small follow-up; that file is contended by the #305 / rossoctl/moca-experiments#8 work right now.

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.

sandbox-relay: an in-stream ExecError returns a gRPC OK status, so a failed Exec counts as a success

2 participants