Repository navigation
fix(server): record a cancelled Responses stream as cancelled - #2870
Conversation
The cancel route answered `cancelled`, but the streaming task decided the final status from `finish_reason` alone, so the terminal SSE event and the stored copy said `completed`. Check the cancellation token when the stream finishes and report `ResponseStatus::Cancelled`.
|
|
A stopped stream now ends with a new `response.cancelled` terminal event instead of `response.completed`, with `completed_at` null and no `incomplete_details`. A cancel that lands before the first token ended generation with an error, so the client saw `server_error` and the id 404ed after the route had answered `cancelled`. The acknowledged cancel is now recorded and reported as cancelled, without touching the conversation. The registry now records which cancels were acknowledged, and the stream retires its entry before deciding the status, so a cancel can no longer be answered `cancelled` after a `completed` was chosen. A client disconnect after generation has ended no longer flips the token, so a finished response stays completed. The stored copy is written before the terminal event.
The OpenAI Responses event set has no cancelled terminal event, and SDK helpers such as openai-python `get_final_response()` succeed only on `response.completed`. A cancelled stream now ends with `response.completed` whose `response.status` is `cancelled`, so clients must check `status`. `completed_at` stays null. A cancel before the first token now reports and stores an empty `output`. The response assembly added an empty message item for it.
|
Hardening review, pushed as Findings fixed
Maintainer decision: the Security and performance: no new input surface. Validation on GB10: 175 responses tests pass, and streaming and cancel ran on Merge gate: first-time-contributor workflow runs wait for approval and were not started, so by maintainer decision the gate ran on GB10 against one tree integrating #2888, #2872, #2869, #2871 and #2870 on |
Summary
Cancelling an in-flight
/v1/responsesstream answers200 {"status": "cancelled"}, but the streaming task then finishes through the normal path and decides its final status fromfinish_reasonalone (final_statusinresponses_auto_compact.rs). The terminal SSE event and the stored copy therefore saycompleted, contradicting the cancel reply.ResponseStatus::Cancelledexists but is never assigned.The streaming task now keeps a handle to the cancellation token and reports
ResponseStatus::Cancelledwhen the token is set at the end of the stream (stream_create_response,responses.rs:627and:1100).Related issues
No linked issue.
Type of change
fix— bug fixTest plan
cargo fmt --all -- --checkcargo clippy --workspace --all-targets --features metal,accelerate -- -D warningscargo test -p mlxcel --lib --profile test-fast --features metal,accelerate records_the_response_as_cancelled: the new test fails before the fix (left: "completed",right: "cancelled") and passes after it.cargo test -p mlxcel --lib --profile test-fast --features metal,accelerate responses: 170 passed.cargo test --workspace --profile test-fast --features metal,accelerate --no-fail-fast -- --test-threads=1: not run locally.cargo deny check: not run; no dependency or manifest changes.mlx-community/Qwen3-0.6B-4bit, macOS Apple Silicon, Metal): stream a long answer, cancel it mid-stream, then read the terminal SSE event andGET /v1/responses/{id}. An uncancelled stream still endscompleted.cancelledcancelledcompletedcancelledcompletedcancelledcompletedcompletedNotes for reviewers
The terminal SSE event keeps its
response.completedtype; only thestatusinside it changes.Checklist
feat:,fix:, etc.)docs/if user-facing behavior or supported models changed: not needed;docs/responses-api.mddescribes cancel as best-effort and does not state the final status.// Used by: ...comments on any shared function I modified (seedocs/code-guidelines.md): no shared function changed..envfiles committed