Skip to content

#777 Fix iOS crash: WorktreesAPIClient used an invalidated URLSession - #778

Merged
ivanfenenko merged 1 commit into
mainfrom
fix/777-worktrees-client-invalidated-session
Oct 21, 2026
Merged

ivanfenenko merged 1 commit into
mainfrom
fix/777-worktrees-client-invalidated-session

Conversation

@ivanfenenko

@ivanfenenko ivanfenenko commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #777

Root cause

WorktreesAPIClient was the only REST client still capturing OpenCodeURLSessions.rest at init (#704 merged alongside #703, which added the live-pool resolver to the other clients). It is a singleton in AppContainer. When the SSE connect budget (5s) is exceeded, EventsAPIClient calls OpenCodeURLSessions.replaceRest(), which invalidates the old pool. From then on, every worktree request (Home reloadWorktreeMarks, SessionChatViewModel.resolveCheckoutName → DefaultWorktreeRepository.loadV2 → listV2) creates a task on the invalidated session. That raises an uncatchable NSGenericException, and the app keeps crashing until it is relaunched.

Crashlytics issue 3bf8d36d8cf8888eeafecfa7e7f27f46: 3 events, 2 users, 1.1.0 (35). Breadcrumbs show it firing 14s after launch, and in a crash loop for one user on a flaky connection.

Changes

  • WorktreesAPIClient: urlSession: URLSession? = nil plus OpenCodeURLSessions.restResolver(injected:), the same as the other REST clients. listV2, refreshV2 and probeV1 now resolve the pool for each request, and it gets a resolvedURLSession() accessor for tests.
  • OpenCodeURLSessions.replaceRest(): closes a remaining race. A caller could read the old pool and get its task created only after invalidateAndCancel() had run, which is the same crash. The old pool's in-flight tasks are still cancelled right away, so the intent of Reconnect without restarting when live updates stall #701/#701 Drop the stale REST pool when live updates stall #703 is unchanged. Invalidation now waits a 5s grace period.
  • I checked the iOS app for other URLSessions stored at init and later invalidated, and found none. EventsAPIClient and URLSessionTerminalSocketClient own and invalidate their own sessions (fixed in #745 Never create an SSE task on an invalidated URLSession #747). The connection probe and pairing sessions are static and never invalidated. Feedback uses .shared.

Android has no equivalent pool replacement, so it needs no change.

Checks

  • xcodebuild test on an iPhone 17 simulator (iOS 26.5), suites OpenCodeURLSessionsTests, WorktreeMarkTests and WorkspaceDirectoryParserTests: 12/12 passed.
  • New tests:
    • A live WorktreesAPIClient follows the replaced pool, and an injected one stays fixed.
    • A task can still be created on the previous pool right after replaceRest().
  • Confirmed the straggler test catches the bug: with the old immediate invalidateAndCancel(), the test host crashes with the exact Crashlytics error, "Task created in a session that has been invalidated".
  • No UI change and no screen evidence. This is a crash fix covered by the unit tests above.

Risks / Follow-ups

  • After a swap, the old pool stays valid for up to 5s. A request that starts on it in that window still runs, then gets cancelled when the pool is invalidated, and the caller sees a normal cancellation error.
  • The grace period makes the race far less likely but does not rule it out completely. It would take more than 5s to pass between reading the pool and creating the task.
  • Not run against a real server with a forced connect-budget timeout.

🤖 Generated with Claude Code


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

WorktreesAPIClient captured OpenCodeURLSessions.rest at init, so after the
SSE connect budget called replaceRest() every worktree request created a
task on the invalidated pool and crashed with NSGenericException.

Read the live pool at request time like the other REST clients, and give
replaceRest() a grace period: cancel the previous pool's tasks now and
invalidate it a few seconds later, so a caller that read the old pool just
before the swap cannot crash either.

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: NSGenericException "Task created in a session that has been invalidated" (WorktreesAPIClient / EventsAPIClient)

1 participant