fix(cloudflare): Use TransformStream to keep track of streams#20452
Merged
fix(cloudflare): Use TransformStream to keep track of streams#20452
Conversation
Contributor
size-limit report 📦
|
75e9262 to
6de2693
Compare
6de2693 to
339219a
Compare
logaretm
approved these changes
Apr 22, 2026
Member
logaretm
left a comment
There was a problem hiding this comment.
Way cleaner, that's very cool!
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.
closes #20409
closes JS-2233
Problem
The issue is that we had a
waitUntil?.(streamMonitor);that waited until the stream was done.streamMonitorcould potentially live forever, whilewaitUntilhas a limit of 30 seconds until it is getting cancelled. With the current approach we allow streams only being open for 30 seconds - then thewaitUntilwould cancel the request. This can only be reproduced when deployed, that is the reason why we didn't notice in our test cases.Solution
By removing the
waitUntilthere is no hard limit, but we still have to wait until the stream is over in order to end our spans and flush accordingly. This can be achieved with TransformStream, where we simply use the stream body from the client and pipe it through our transformer. Withflushandcancelwe know exactly when the stream would end or be cancelled - which is the only thing we need.There is btw no reason to add integration of E2E tests, as
miniflaredoesn't have this limitation and it couldn't be reproduced, so the tests would always succeed. The unit tests are kinda simulating that by checking ifwaitUntilis getting called or not.I also figured that the client isn't getting disposed and would leak memory - this PR is also fixing that (see screenshots).
Some evidence
repro: https://github.com/JPeer264/sentry-repros/tree/issue-20409
cloudflare logs before:
cloudflare logs after:
Sentry trace: https://sentry-sdks.sentry.io/explore/traces/trace/29a307b1272e48dbb3a87c270c487e5a/