Repository navigation
#777 Fix iOS crash: WorktreesAPIClient used an invalidated URLSession - #778
Merged
Merged
Conversation
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]>
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 #777
Root cause
WorktreesAPIClientwas the only REST client still capturingOpenCodeURLSessions.restat init (#704 merged alongside #703, which added the live-pool resolver to the other clients). It is a singleton inAppContainer. When the SSE connect budget (5s) is exceeded,EventsAPIClientcallsOpenCodeURLSessions.replaceRest(), which invalidates the old pool. From then on, every worktree request (HomereloadWorktreeMarks,SessionChatViewModel.resolveCheckoutName→DefaultWorktreeRepository.loadV2→listV2) creates a task on the invalidated session. That raises an uncatchableNSGenericException, 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? = nilplusOpenCodeURLSessions.restResolver(injected:), the same as the other REST clients.listV2,refreshV2andprobeV1now resolve the pool for each request, and it gets aresolvedURLSession()accessor for tests.OpenCodeURLSessions.replaceRest(): closes a remaining race. A caller could read the old pool and get its task created only afterinvalidateAndCancel()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.URLSessions stored at init and later invalidated, and found none.EventsAPIClientandURLSessionTerminalSocketClientown 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 teston an iPhone 17 simulator (iOS 26.5), suitesOpenCodeURLSessionsTests,WorktreeMarkTestsandWorkspaceDirectoryParserTests: 12/12 passed.WorktreesAPIClientfollows the replaced pool, and an injected one stays fixed.replaceRest().invalidateAndCancel(), the test host crashes with the exact Crashlytics error, "Task created in a session that has been invalidated".Risks / Follow-ups
🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmithwith what you need.