Skip to content

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

Closed

Description

@ivanfenenko

New fatal crash on the newest iOS build with sessions, 1.0.1 (34). It first appeared on 2026-10-19.

Crashlytics c093ceab5d92f7775252cc49bcf001c6: NSGenericException: Task created in a session that has been invalidated at closure #2 in EventsAPIClient.streamEvents(connection:path:directory:connectBudget:sessionToken:onHTTPResponse:onActivity:onDataLine:) (EventsAPIClient.swift:195 in build 34). That line is let (bytes, response) = try await session.bytes(for: request) inside bytesTask. On main it is line 202.

  • Events: 1 event, 1 user (90d), all on 1.0.1 (34); last event 2026-10-19 12:04 CEST
  • Device: iPhone17,1 (iPhone 16 Pro), iOS 26.6.1, foreground
  • Stack (truncated): -[__NSURLSessionLocal taskForClassInfo:] -> objc_exception_throw -> std::__terminate -> Kotlin/Crashlytics terminate handlers. This is an Objective-C exception, so Swift try/catch can't catch it.

Likely cause: streamEvents gets its URLSession from openStreamingSession(token:) and then starts session.bytes(for:) later, in a detached Task. That session can already be invalidated when the task is created:

  1. openStreamingSession calls invalidateAndCancel() on the new session when token < retiredBefore but still returns it.
  2. installFreshStreamingSession / retireStreamingSession (or deinit) can run invalidateAndCancel() on owned dials between openStreamingSession and the bytesTask body, for example on token rotation or stall recovery (Reconnect without restarting when live updates stall #701/#701 Drop the stale REST pool when live updates stall #703).

Creating a task on an invalidated URLSession raises NSGenericException and kills the process. The code is unchanged on main @ 7ad92e7 (#703 only added onConnectBudgetExceeded), so iOS release 35 has it too.

Console: https://console.firebase.google.com/project/opencode-mobile-release/crashlytics/app/ios:dev.opencode.mobile/issues/c093ceab5d92f7775252cc49bcf001c6

Outcome: SSE dials never start a task on an invalidated URLSession. A retired or rejected dial ends as a normal transport/cancel outcome instead of crashing.

Remaining:

  • Don't return an invalidated session from openStreamingSession: throw, or return nil, when the token is rejected
  • Hold the dial lease, or re-check that it is still owned under sessionLock, before session.bytes(for:), and map a retired session to CancellationError / .transport
  • Add a test that retires the streaming session between open and dial

Crashlytics: c093ceab5d92f7775252cc49bcf001c6

Activity

  1. added
    bug
    ios
    priority: high
    crash
    on Oct 20, 2026
  2. added a commit that references this issue on Oct 20, 2026
  3. ivanfenenko commented on Oct 21, 2026

    @ivanfenenko
    CollaboratorAuthor

    Crashlytics triage, 2026-10-21: the fix PR #747 (DialLease, never start an SSE task on an invalidated URLSession, merge commit 0485c17) was merged on 2026-10-20 at 11:27 CEST.

    Console: https://console.firebase.google.com/project/opencode-mobile-release/crashlytics/app/ios:dev.opencode.mobile/issues/c093ceab5d92f7775252cc49bcf001c6

  4. ivanfenenko commented on Oct 26, 2026

    @ivanfenenko
    CollaboratorAuthor

    Crashlytics triage, 2026-10-26: the #747 fix (0485c17f57) now ships in iOS Release run 36 (f083701be5, 2026-10-25 22:28 CET, https://github.com/opencode-mobile/opencode-mobile/actions/runs/40235676062). c093ceab has had no new events since the last check. Crashlytics shows no sessions for build 36 yet, so closure is still unconfirmed. Earliest confirmation is around 2026-11-02, if build 36 has ~50+ sessions and 0 events by then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcrashiospriority: high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions