fix(functions): invalidate URLSession in streamed response - #1122
Open
AndroidPoet wants to merge 3 commits into
Open
fix(functions): invalidate URLSession in streamed response#1122AndroidPoet wants to merge 3 commits into
AndroidPoet wants to merge 3 commits into
Conversation
The streamed-response URLSession is delegate-backed, so it retained its StreamResponseDelegate (and transitively the stream continuation) until invalidated. onTermination only cancelled the task and never invalidated the session, leaking a session + delegate on every streamed invocation. Invalidate the session on termination instead, which also cancels the outstanding task.
Port the streamed-response regression test to Swift Testing, which main now requires. Without the @test attribute the merged test would not have run. Spell FunctionInvokeOptions explicitly at the call site, since contextual .init() is now ambiguous between the disfavored overloads.
Contributor
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughSummary by CodeRabbit
Walkthrough
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
What
_invokeWithStreamedResponsecreates a per-call, delegate-backedURLSession. A delegate-backedURLSessionkeeps a strong reference to its delegate until the session is explicitly invalidated (Apple docs: "the session object keeps a strong reference to the delegate until your app exits or explicitly invalidates the session").Today
onTerminationonly cancels the task and holds_ = delegate, but never invalidates the session:So every streamed invocation leaks a
URLSessionand itsStreamResponseDelegate(which retains the stream continuation) permanently — even after the caller stops consuming. Apps that open streaming edge functions repeatedly accumulate them unbounded.Fix
Invalidate the session when the stream terminates.
invalidateAndCancel()cancels the outstanding task and releases the session's reference to the delegate, sotask.cancel()/ the_ = delegatehack are no longer needed:This mirrors the fix already merged for the Realtime web-socket session in #1080 (
fix(realtime): invalidate URLSession when web socket closes).Testing
Added
testInvokeWithStreamedResponseInvalidatesSession, which weak-references the stream delegate and asserts it deallocates after the stream terminates. It fails onmain(delegate leaks — assertion fails after the poll window) and passes with this change (delegate freed almost immediately). To observe the delegate's lifetime the streamed setup is factored into aninternalstreamResponse(_:options:)that also returns the delegate; the public_invokeWithStreamedResponsesignature is unchanged.swift test --filter FunctionsTests— 29 passing./scripts/format.sh— clean