-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(cloudflare): Keep http root span alive until streaming responses are consumed #18087
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RulaKhaled
wants to merge
9
commits into
develop
Choose a base branch
from
fix-streaming
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2b3eaf1
try to override p-map till react router esolves this issue
RulaKhaled 0842114
fix(cloudflare): Keep root span alive until streaming responses are c…
RulaKhaled 7a570db
Merge branch 'develop' into fix-streaming
RulaKhaled eb7a5c1
quick refactor
RulaKhaled ae8e345
test buffered without timeout
RulaKhaled 62a1b2c
remove timeout
RulaKhaled ca4bafa
give this a shot
RulaKhaled 31ecf58
revert and fix tests
RulaKhaled 17a03a7
fix test
RulaKhaled File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| export type StreamingGuess = { | ||
| response: Response; | ||
| isStreaming: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Classifies a Response as streaming or non-streaming. | ||
| * | ||
| * Heuristics: | ||
| * - No body → not streaming | ||
| * - Known streaming Content-Types → streaming (SSE, NDJSON, JSON streaming) | ||
| * - text/plain without Content-Length → streaming (some AI APIs) | ||
| * - Otherwise → not streaming (conservative default, including HTML/SSR) | ||
| * | ||
| * We avoid probing the stream to prevent blocking on transform streams (like injectTraceMetaTags) | ||
| * or SSR streams that may not have data ready immediately. | ||
| */ | ||
| export function classifyResponseStreaming(res: Response): StreamingGuess { | ||
| if (!res.body) { | ||
| return { response: res, isStreaming: false }; | ||
| } | ||
|
|
||
| const contentType = res.headers.get('content-type') ?? ''; | ||
| const contentLength = res.headers.get('content-length'); | ||
|
|
||
| // Streaming: Known streaming content types | ||
| // - text/event-stream: Server-Sent Events (Vercel AI SDK, real-time APIs) | ||
| // - application/x-ndjson, application/ndjson: Newline-delimited JSON | ||
| // - application/stream+json: JSON streaming | ||
| // - text/plain (without Content-Length): Some AI APIs use this for streaming text | ||
| if ( | ||
| /^text\/event-stream\b/i.test(contentType) || | ||
| /^application\/(x-)?ndjson\b/i.test(contentType) || | ||
| /^application\/stream\+json\b/i.test(contentType) || | ||
| (/^text\/plain\b/i.test(contentType) && !contentLength) | ||
| ) { | ||
| return { response: res, isStreaming: true }; | ||
| } | ||
|
|
||
| // Default: treat as non-streaming | ||
| return { response: res, isStreaming: false }; | ||
| } |
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Missing error handling for
response.body.tee()call.Severity: MEDIUM | Confidence: Low
🔍 Detailed Analysis
The call to
classification.response.body.tee()is not wrapped in error handling. Iftee()throws (e.g., if the body stream is locked or in an invalid state), this will result in an uncaught exception. This bypasses existing error handling, preventing proper span termination and error capture by Sentry's exception handler.💡 Suggested Fix
Wrap the
classification.response.body.tee()call in atry-catchblock to handle potentialTypeErrorexceptions gracefully.🤖 Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID:
2881816