Skip to content

fix(execution): stop a run with an explicit Cancel, not only by closing the stream - #9

Merged
0xmhha merged 1 commit into
mainfrom
fix/cancel-on-stop
Oct 29, 2026
Merged

0xmhha merged 1 commit into
mainfrom
fix/cancel-on-stop

Conversation

@0xmhha

@0xmhha 0xmhha commented Oct 29, 2026

Copy link
Copy Markdown
Member

Summary

Stop now always sends ExecutionService.Cancel(execution_id) instead of only closing the Run stream.

The backend is changing so that closing a Run stream no longer stops the run, which is what lets ResumeRun recover output after a network drop. Before this change, Stop only aborted the stream, except while reconnecting. After the backend change, a stopped program would therefore keep running on the server until its time limit.

Design (src/lib/rpc/run-stream.ts)

  • runSession now owns the stream's AbortController. The caller's signal (Stop, reset() or unmount) no longer closes the stream directly. Instead it triggers the following:
    • After Accepted: send Cancel(execution_id), then close the stream.
    • Before Accepted: the execution id is not known yet. The stream stays open for up to ACCEPT_GRACE_MS (3 s), so the client can learn the id when Accepted arrives and then cancel. If Accepted never arrives, the stream is closed without a Cancel.
    • While reconnecting (ResumeRun or polling): send Cancel, as before. This now also applies when Stop comes during the last retry.
  • Cancel is sent at most once per run and is not tied to the already-aborted signal.
  • The UI never waits long: the result changes to cancelled once Cancel answers, or after CANCEL_WAIT_MS (1 s) at most. A failing Cancel cannot leave Stop hanging.
  • Finished runs are left alone: a run that has already finished is never cancelled, even if its component unmounts later.

Testing

  • npm run test:coverage: 437 tests pass and the thresholds are met. New tests:
    • Stop mid-stream: Cancel is called once with the execution id, the stream closes and the status is cancelled.
    • Stop before Accepted: no Cancel is sent until Accepted arrives, then Cancel is sent.
    • Accepted never arrives: the stream closes after the grace period and no Cancel is sent.
    • Cancel fails: the run still ends as cancelled.
    • Finished run: a later abort does not call Cancel.
    • Hook level: useCodeExecution().cancel() reaches the server's Cancel handler.
  • npx playwright test tests/e2e/fake: 22 tests pass, including the existing "Stop ends a long run".
  • npm run typecheck: passes.
  • npm run lint: 0 errors and 12 warnings, as on main.

Not verified

  • I did not test against the backend version in which closing the stream no longer cancels, because it is not released yet.

…ng the stream

The backend is changing so that closing a Run stream no longer stops
the run. This is what lets ResumeRun recover the output after a network
drop. Until now Stop only aborted the stream, except while reconnecting,
so after that change a stopped program would keep running on the
server.

runSession now owns the stream's AbortController. When the caller's
signal aborts (Stop, reset, unmount):

- After Accepted: send Cancel(execution_id), then close the stream.
- Before Accepted: keep the stream open for up to 3 s to learn the
  execution id, then cancel. If the id never arrives, close the stream.
- While reconnecting or polling: send Cancel, as before.

The result changes to "cancelled" once Cancel answers, or after at most
1 s, so a failing Cancel never leaves the UI stuck. A run that already
finished is never cancelled.
@0xmhha
0xmhha merged commit 95d66dc into main Oct 29, 2026
2 checks passed
@0xmhha
0xmhha deleted the fix/cancel-on-stop branch October 29, 2026 03:47
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