Repository navigation
#745 Never create an SSE task on an invalidated URLSession - #747
Merged
Merged
Conversation
openStreamingSession invalidated a session for a retired token but still returned it, and installFreshStreamingSession / retireStreamingSession could invalidate an owned dial between open and session.bytes(for:). Either path raised NSGenericException "Task created in a session that has been invalidated", which Swift cannot catch. Each dial now holds a DialLease. A retired token gets no session, and a retire while the dial may be creating its task cancels the dial instead of invalidating; the dial's own exit invalidates. Retired dials end as .transport(URLError(.cancelled)), as an invalidateAndCancel of a dialing task did before, so stall recovery and token rotation keep working. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Oct 21, 2026
Closed
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.
Fixes #745
Problem
iOS 1.0.1 (34) crashed with
NSGenericException: Task created in a session that has been invalidatedatsession.bytes(for:)inEventsAPIClient.streamEvents. Swiftcatchcan't stop that exception. It had two causes:openStreamingSession(token:)calledinvalidateAndCancel()on a session for a retired token, then still returned it.installFreshStreamingSession,retireStreamingSession(s)(token rotation, stall recovery Reconnect without restarting when live updates stall #701/#701 Drop the stale REST pool when live updates stall #703,stop()) could invalidate an owned dial after it was opened but before its task was created.Fix
DialLease(token, session, phase, retired flag).ownedDialsstores leases instead of bare sessions.openStreamingSessionreturnsnilandstreamEventsreturns.transport(URLError(.cancelled)).beginDial()checks the retired flag and switches to.dialingunder the lease lock. A retire during.openedor.streamingstill invalidates immediately, as before. A retire during.dialingonly cancels the dial's Swift task. The dial's own exit (finishOwned) then invalidates, so a task is never created on an invalidated session..transport(URLError(.cancelled)), which is whatinvalidateAndCancel()of a dialing task produced before. Parent cancellation (stop()) still throwsCancellationError, and connect-budget handling is unchanged. Stall recovery and token rotation keep their reconnect behaviour.global/event,/event) and v2 (api/event) paths share the same code. The new tests run on both paths.streamingSessionFactoryandbeforeDialon the owned-sessioninit(defaults are unchanged for the app).Tests
New tests in
EventsAPIClientTests, each run with bothglobal/eventandapi/event:streamEvents_retiredTokenDialEndsAsTransport: a dial for an already-retired token never reaches the network.streamEvents_retiredBetweenOpenAndDialEndsAsTransport: the iOS crash: URLSession task created in invalidated session in EventsAPIClient.streamEvents #745 race. A retire runs between open and dial. I checked that this test reproduces the exact crash (Task created in a session that has been invalidated (NSGenericException)) when the lease guards are disabled.streamEvents_tokenRotationWhileDialingEndsAsTransport: token rotation during a hung handshake ends the dial quickly as.transport.retireStreamingSessions_rejectsLaterOpenOfOlderTokennow also asserts that a rejected open returnsnil.Local run on an iPhone 16 Pro Max simulator:
EventsAPIClientTests,DefaultForegroundEventStreamTests,SseRecoveryFaultOracleTests,SseAttemptPolicyTests,SseDiagnosticLoggingTests— 59 tests in 5 suites passed.🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmithwith what you need.