Skip to content

iOS crash: NSGenericException "Task created in a session that has been invalidated" (WorktreesAPIClient / EventsAPIClient) #777

Closed

Description

@ivanfenenko

Fatal iOS crash reported by Crashlytics.

New issue: WorktreesAPIClient.data(for:) throws NSGenericException "Task created in a session that has been invalidated".

  • Version: 1.1.0 (35) only
  • 3 events, 2 users, first seen Oct 20, 2026 (UTC)
  • Crashlytics issue

Related issue, same error: EventsAPIClient.streamEvents on 1.0.1 (34), 1 event, 1 user, first seen Oct 19, 2026 (Crashlytics).

Hypothesis (not verified against the code): a shared URLSession gets invalidated (invalidateAndCancel / finishTasksAndInvalidate, e.g. on reconnect, server switch or logout) while other clients still create tasks on it. Check the session lifecycle and recreate the session, or guard against task creation after invalidation.

Impact: 1.1.0 is rolling out (10 users on Oct 20), so this will grow with adoption.

Activity

  1. added
    bug
    on Oct 21, 2026
  2. ivanfenenko commented on Oct 21, 2026

    @ivanfenenko
    CollaboratorAuthor

    Crashlytics triage, 2026-10-21: code-level root cause for 3bf8d36d8cf8888eeafecfa7e7f27f46.

    Current numbers: 3 events / 2 users, all on 1.1.0 (35). Devices are iPhone15,4 ×2 and iPhone17,3, all on iOS 27.0.1, all in the foreground. The latest event was 2026-10-21 08:35 CEST. Blamed frame: WorktreesAPIClient.data(for:) (WorktreesAPIClient.swift:54) → -[__NSURLSessionLocal taskForClassInfo:] → objc_exception_throw.

    Likely cause (checked against main and 7ad92e7): WorktreesAPIClient captures the REST pool once, at init:

    init(connectionProvider: ..., urlSession: URLSession = OpenCodeURLSessions.rest) {
        self.urlSession = urlSession
    }

    OpenCodeURLSessions.replaceRest() installs a new pool and then calls previous.invalidateAndCancel(). Its doc comment says clients without an injected session "must read this at request time". After any replaceRest(), WorktreesAPIClient keeps calling urlSession.data(for:) (in data(for:) and probeV1) on the old, invalidated session. That throws an ObjC NSGenericException, which Swift can't catch. SessionsAPIClient and ProjectsAPIClient already do this correctly with OpenCodeURLSessions.restResolver(injected:).

    Suggested fix: switch WorktreesAPIClient to resolveURLSession = OpenCodeURLSessions.restResolver(injected: urlSession) with urlSession: URLSession? = nil. Then check the other *APIClients for the same = OpenCodeURLSessions.rest default. Add a test that calls replaceRest() and then makes a worktree request.

    #747 (merged 2026-10-20) fixed only the related EventsAPIClient.streamEvents crash (c093ceab, #745). It does not touch this code path.

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

  3. added a commit that references this issue on Oct 21, 2026
  4. added
    ios
    priority: high
    crash
    on Oct 21, 2026
  5. ivanfenenko commented on Oct 22, 2026

    @ivanfenenko
    CollaboratorAuthor

    Update Oct 22: the same error now also appears in WorktreesAPIClient.probeV1(directory:) on 1.1.0 (35), with 1 event from 1 user (Crashlytics). The WorktreesAPIClient.data(for:) issue is up to 9 events from 5 users this week. Three call sites with the same error point to one shared URLSession lifecycle bug.

  6. ivanfenenko commented on Oct 22, 2026

    @ivanfenenko
    CollaboratorAuthor

    Crashlytics triage, 2026-10-22: fix status.

  7. ivanfenenko commented on Oct 23, 2026

    @ivanfenenko
    CollaboratorAuthor

    Crashlytics triage, 2026-10-23: sharp spike, fix still unshipped.

  8. ivanfenenko commented on Oct 26, 2026

    @ivanfenenko
    CollaboratorAuthor

    Crashlytics triage, 2026-10-26: the spike is continuing, and the fix is now in a release build.

    • 3bf8d36d8cf8888eeafecfa7e7f27f46 WorktreesAPIClient.data(for:): went from 22 events / 13 users on 2026-10-23 to 45 events / 19 users, all on 1.1.0 (35).
      • By OS: iOS 27.0.1 (26), 26.6.2 (6), 18.6.2 (5), 26.6.1 (4), 26.5.2 (2), 27.0.0 (1), 18.7.2 (1).
      • Latest event: 2026-10-26 07:30 CET.
    • ccf83d3cdb996ef5313300491f420dab WorktreesAPIClient.probeV1(directory:), which has the same root cause: went from 1 / 1 to 20 events / 3 users on 1.1.0 (35). 16 of those events come from one iPhone15,4 on iOS 27.2.0. Latest event: 2026-10-24 21:03 CET.
    • Build 35 has 891 sessions over 8 days and 65 events between these two issues, which is about 7% of sessions.
    • iOS Release run 36 (f083701be5, 2026-10-25 22:28 CET, https://github.com/opencode-mobile/opencode-mobile/actions/runs/40235676062) contains the fix from #777 Fix iOS crash: WorktreesAPIClient used an invalidated URLSession #778 (f2e58ba125, 33 commits behind the run head) and the #745 Never create an SSE task on an invalidated URLSession #747 fix. Crashlytics shows no sessions for build 36 yet. I'll confirm closure once it has been out ≥7 days with ~50+ sessions and 0 events.
  9. ivanfenenko commented on Oct 27, 2026

    @ivanfenenko
    CollaboratorAuthor

    Crashlytics triage, 2026-10-27: the spike is continuing on 1.1.0 (35).

    • 3bf8d36d8cf8888eeafecfa7e7f27f46 WorktreesAPIClient.data(for:) went from 45 events / 19 users on 2026-10-26 to 63 events / 25 users, all on 1.1.0 (35). The latest event was 2026-10-27 at 03:13 CET.
      • By OS: iOS 27.0.1 (32), 26.6.2 (7), 18.6.2 (6), 27.2.0 (5), 26.6.1 (5), 26.5.2 (4), 18.7.10 (2), 27.0.0 (1), 18.7.2 (1).
    • ccf83d3cdb996ef5313300491f420dab WorktreesAPIClient.probeV1(directory:) is unchanged at 20 events / 3 users on 1.1.0 (35). Its last event was 2026-10-24.
    • 1.1.0 (35) now has 1048 sessions over 9 days. The two crashes account for 83 events on that build.
    • The fix from #777 Fix iOS crash: WorktreesAPIClient used an invalidated URLSession #778 (f2e58ba125) is in iOS Release run 36, which is 1.2.0 (f083701be5). Crashlytics shows no sessions or events for build 36 yet, so there's no sign of this crash on a build that has the fix. The spike should only stop once users move to 1.2.0 (36).

    If either crash appears on 1.2.0 (36) or later, I'll reopen this issue.

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