Skip to content

#745 Never create an SSE task on an invalidated URLSession - #747

Merged
ivanfenenko merged 1 commit into
mainfrom
fix/745-ios-sse-invalidated-session-crash
Oct 20, 2026
Merged

ivanfenenko merged 1 commit into
mainfrom
fix/745-ios-sse-invalidated-session-crash

Conversation

@ivanfenenko

@ivanfenenko ivanfenenko commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #745

Problem

iOS 1.0.1 (34) crashed with NSGenericException: Task created in a session that has been invalidated at session.bytes(for:) in EventsAPIClient.streamEvents. Swift catch can't stop that exception. It had two causes:

  1. openStreamingSession(token:) called invalidateAndCancel() on a session for a retired token, then still returned it.
  2. 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

  • Each streaming dial now holds a DialLease (token, session, phase, retired flag). ownedDials stores leases instead of bare sessions.
  • A retired token gets no session: openStreamingSession returns nil and streamEvents returns .transport(URLError(.cancelled)).
  • beginDial() checks the retired flag and switches to .dialing under the lease lock. A retire during .opened or .streaming still invalidates immediately, as before. A retire during .dialing only cancels the dial's Swift task. The dial's own exit (finishOwned) then invalidates, so a task is never created on an invalidated session.
  • A dial retired before headers ends as .transport(URLError(.cancelled)), which is what invalidateAndCancel() of a dialing task produced before. Parent cancellation (stop()) still throws CancellationError, and connect-budget handling is unchanged. Stall recovery and token rotation keep their reconnect behaviour.
  • The v1 (global/event, /event) and v2 (api/event) paths share the same code. The new tests run on both paths.
  • Test seams: streamingSessionFactory and beforeDial on the owned-session init (defaults are unchanged for the app).

Tests

New tests in EventsAPIClientTests, each run with both global/event and api/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_rejectsLaterOpenOfOlderToken now also asserts that a rejected open returns nil.

Local run on an iPhone 16 Pro Max simulator: EventsAPIClientTests, DefaultForegroundEventStreamTests, SseRecoveryFaultOracleTests, SseAttemptPolicyTests, SseDiagnosticLoggingTests — 59 tests in 5 suites passed.

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith with what you need.

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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

iOS crash: URLSession task created in invalidated session in EventsAPIClient.streamEvents

1 participant