Skip to content

fix: Return the full Response from background cancel #2899

Closed

Description

@inureyes

Part of #2903

Context

Line numbers are at origin/main 8d3e16ad. cancel_response (src/server/routes/responses.rs:1300) answers an in-flight stream with a minimal id, object, status envelope. #2893 and #2894 made a repeat cancel of a stored cancelled response return the stored object; any other finished response returns 400. The OpenAI contract (https://developers.openai.com/api/docs/guides/background.md): cancel is idempotent and returns the final Response. A Python SDK client.responses.cancel() on a background job therefore needs a full object.

From #2897, on ResponsesStore: request_background_cancel(id) -> BackgroundCancel { NotFound, Requested, Terminal(ResponseObject) } (flips the token, leaves the job active), finish_background(id, terminal) -> ResponseObject (first writer wins), wait_background_terminal(id, timeout) -> Option<ResponseObject>, background_status(id). From #2898: the driver records the terminal through BackgroundSink::record_terminal, and an acknowledged cancel ends as cancelled.

Scope

In cancel_response, before the foreground branches:

  • Queued. Finish at once with status: cancelled, output: [], completed_at: null through finish_background, flip the token, return the object. The job is never started, or stops at its next token check.
  • In progress. request_background_cancel, then wait_background_terminal for at most BACKGROUND_CANCEL_WAIT (a 5 second constant, proposed default), which keeps the partial output the driver records. On timeout, call finish_background with a cancelled copy of the snapshot and return that; a late driver write is a no-op.
  • Repeat. A job already cancelled returns the same stored object, unchanged.
  • Race with completion. One object wins through finish_background. If completed, incomplete, or failed won, return the existing 400 has already finished and cannot be cancelled, matching fix(server): align context overflow code and repeat cancel with OpenAI #2894's rule for finished responses. Never answer cancelled for a response GET reports as completed.
  • store: false jobs behave the same, since background_snapshot reads ephemeral entries; a cancelled store: false job stays readable for the retention window only.

A foreground stream keeps its minimal envelope and its existing tests, so foreground behavior is byte-compatible.

Cancelling frees the job slot at the terminal write. The blocking task may still run until it next checks the token; it holds no job slot.

Out of scope

Stream events after cancel (the streaming sub-issue), foreground cancel changes.

Acceptance criteria

Tests in src/server/routes/responses_background_route_tests.rs on the scripted provider:

  • Cancel while queued returns the full cancelled Response, and the job never generates a token afterwards.
  • Cancel while in_progress returns the full object including partial output.
  • The cancel body equals a following GET body byte for byte.
  • A repeat cancel returns an identical body.
  • A cancel/complete race run 200 times always yields a cancel reply consistent with GET.
  • Cancel after completed returns 400 and leaves GET unchanged.
  • A driver that ignores the token past the wait: cancel returns cancelled, and the late terminal write does not change GET.
  • Unknown id returns 404.
  • Existing responses_cancel_route_tests.rs passes unchanged.
  • active_background_jobs() is 0 after each case.
  • A cancel that stops generation before the first token (the driver's error path) is recorded cancelled, not failed (reviewer MEDIUM from feat(responses): create and poll background responses #2919; JobRecorder::fail).

Dependencies

#2898.

Verification

cargo fmt --all -- --check
cargo clippy --all-targets --features cuda -- -D warnings  # metal,accelerate on macOS
cargo test --lib --profile test-fast --features cuda server::routes::responses -- --test-threads=1

Manual: on qwen3-0.6b-4bit, cancel a long background job with curl and compare the reply with GET.

Activity

  1. added
    status:ready
    type:bug
    priority:medium
    status:in-progress
    status:review
    and removed
    status:ready
    status:in-progress
    on Nov 9, 2026
  2. added
    status:done
    and removed
    status:review
    on Nov 10, 2026
  3. added a commit that references this issue on Nov 13, 2026
    92fba39
  4. added this to the 0.9 milestone on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority:mediumstatus:donetype:bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions