Skip to content

fix(api): OpenRouter ignores caller cancellation and the configured request timeout #2509

Open

Description

@easonLiangWorldedtech

Symptom

OpenRouter does not honour the caller's metadata.abortSignal and the configured request timeout the way the merged providers do. Cancelling a request mid-flight does not stop the model lookup, the SDK call, or the stream, and a completion that lands after the cancel is still delivered to the caller.

Evidence on main

  • The provider does not race its SDK promise through rejectOnAbort (src/api/providers/utils/abort-signal.ts:226), so a cancellation during SDK retry backoff or a request that never settles waits for the transport.
  • The caller signal is handed to the SDK instead of a per-request bridge. The pinned openai SDK does signal.addEventListener('abort', () => controller.abort()) in client.mjs and never removes the listener, so a long-lived caller signal accumulates one listener and one request controller per request.
  • Nothing re-checks the signal after the stream ends, so an iterator that closes without throwing - which the SDK does when it converts an aborted read into a normal end - reports success.
  • src/api/providers/openrouter.ts has no isRequestAborted call anywhere, so an abort the SDK reports on its own is wrapped as a completion failure instead of normalised.

Expected

The provider follows the shared contract exactly as requesty.ts (#1537) and zoo-gateway.ts (#2430) do: bridge the caller signal and dispose it on every exit path, race the SDK promise and the model lookup through rejectOnAbort / resolveModelWithAbort, guard the emit loop and the post-loop end, and normalise an SDK-side abort with isRequestAborted so a cancellation surfaces as AbortError with the message The <provider> request was aborted.

Scope

Provider file plus its spec. No change to utils/abort-signal.ts or utils/request-timeout.ts; the timeout-versus-body-consumption question is tracked separately in #2486.

Acceptance criteria

Derived from the Expected and Evidence sections above; they exist so a PR can cite them rather than paraphrase
them. Each names the observable, not the implementation.

  • AC-1 The caller's metadata.abortSignal is bridged per request and the bridge is disposed on every exit
    path (completion, failure, abort, late result), so the caller signal accumulates no listener across requests.
  • AC-2 The SDK promise and the model lookup are raced through rejectOnAbort / resolveModelWithAbort, so a
    cancellation settles the await instead of waiting for the transport.
  • AC-3 The emit loop and the post-loop end are guarded, so an iterator that closes without throwing after an
    abort does not report success.
  • AC-4 An abort reported by the SDK is normalised with isRequestAborted and surfaces as an AbortError
    whose message is The openrouter request was aborted.
  • AC-5 A completion that resolves after the caller aborted is not delivered to the caller.
  • AC-6 (scope) Changes stay inside the provider file and its spec. utils/abort-signal.ts and
    utils/request-timeout.ts are untouched; the timeout-versus-body-consumption question stays in refactor(api): move hand-written abort bridges to the shared helper #2486.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions