Repository navigation
fix(anthropic): End and mark messages.stream() span as cancelled on abort - #26236
Merged
Merged
Conversation
An aborted MessageStream emits only abort and end, never message or error, so the span was never ended and never sent. End it on end too. Co-Authored-By: Rola Abuhasna <rola.abuhasna@sentry.io>
Contributor
size-limit report 📦Compared: a929000 (PR test merge using develop at 2f3c8a9) Against: 2f3c8a9 (develop) · base run #65503 ✅ Baseline matches: this diff contains only this PR's changes.
|
Matches Google GenAI, which sets cancelled when the caller's signal fires. The SDK only emits abort for a caller-initiated abort; its timeouts surface as error. Co-Authored-By: Rola Abuhasna <rola.abuhasna@sentry.io>
Contributor
Optional E2E testsSome optional checks were not successful. They do not block the merge. 9 not successful, 48 successful. See the run.
Successful checks
|
…bort Attaching an abort listener stopped the SDK from raising an unhandled rejection when a caller aborts a stream and never awaits it. Read stream.aborted in the end listener instead, which every supported SDK version (>=0.19.2) exposes. Co-Authored-By: Rola Abuhasna <rola.abuhasna@sentry.io>
Add integration cases against the real SDK for a stream aborted before the response and one aborted mid-stream; both spans were missing from the envelope before the fix. Add a unit case that a normal completion keeps the status unset and ends the span once, and type the generic with aborted instead of casting. Co-Authored-By: Rola Abuhasna <rola.abuhasna@sentry.io>
51 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why: When a
messages.stream()call is aborted, the Anthropic SDK'sMessageStreamemits onlyabortand thenend. It never emitsmessageorerror. We only ended the span onmessageorerror, so an aborted stream's span never ended and was never sent. The SDK always emitsendlast, so we now end the span there too.endStreamSpandoes nothing if the span already ended. The span keeps whatever usage and response data streamed in before the abort.Inside
end, an aborted stream (stream.aborted) is now markedcancelled, the same as Google GenAI when the caller'sabortSignalfires. Only the caller'sstream.abort()or requestsignalsetsaborted. The SDK's request timeouts are only active until the response headers arrive, and they surface asAPIConnectionTimeoutErroronerror, so they still recordinternal_error.We deliberately don't listen for
abort. The SDK raises an unhandled rejection for an abort only when nothing listens for it, so anabortlistener would silence that rejection for apps that abort a stream and never await it.stream.abortedexists in every supported SDK version (>=0.19.2).How to review: The
endlistener ininstrumentMessageStreamis the whole change. I also checked it by hand against the real@anthropic-ai/sdkMessageStream, aborting it mid-stream against a local SSE server without awaiting it. Uninstrumented and with this change, the app gets the same single unhandled rejection. With this change, the span also endscancelledwith the partial usage attached.Note: Under span streaming (the default),
getSimpleStatussendscancelledasokand drops the status message, as it does for every cancelled span in the SDK. Static mode showscancelled. An aborted span still differs from a completed one: it has no finish reason, and if it was aborted before the response, it has no response ID or usage.via Rola Abuhasna.
--
View Session [Sentry]