Skip to content

#1088 Keep one Home view model across server switches - #1089

Merged
ivanfenenko merged 1 commit into
mainfrom
fix/1088-home-view-model-leak
Nov 19, 2026
Merged

ivanfenenko merged 1 commit into
mainfrom
fix/1088-home-view-model-leak

Conversation

@ivanfenenko

@ivanfenenko ivanfenenko commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #1088.

Switching repeatedly between saved servers got slower each time because every switch built another Home view model. AppRootHomeHost.body called Container.makeProjectListHomeViewModel() inline; ProjectListHomeView kept only the first in its @State, and the rest were retained forever by the endless Task in startObservingConnectionState() (strong self over liveUpdates.updates()). Each leaked model kept a live-updates subscription and left onProfileSaved / onProfileCleared handlers on the shared coordinator. Since a discarded model has pendingID == nil, every profile save made all of them run a full refresh() — probe, worktree listing, a session window per project, cache writes — on the MainActor.

What changed

  • AppRootViewModel now owns Home's model, creates it once on the first enterHome(), and passes it into AppRootHomeHost; nothing is constructed in a view body.
  • startObservingConnectionState() captures self weakly over a snapshot of liveUpdates, and deinit cancels the task, so a discarded model cannot retain itself.
  • ConnectionSessionCoordinator handler registration returns a Registration that removes its handler when cancelled or released (idempotent, lock-guarded, coordinator held weakly). Home's model holds its three registrations.
  • Regression test proves a dropped Home model deallocates; a coordinator test covers cancel and release. ServersSwitchTests now holds its save-counter registration for the fixture's lifetime.

Evidence

Instrumented Debug build against the scenario harness, two saved servers, alternating five times:

Before After
Home models created +3–4 per switch 1 total
Full Home refreshes per switch 5, 9, 13, 17, 21 1, 1, 1, 1, 1
Models deallocated 0 n/a (single app-lifetime model)

The deallocation test fails with the strong capture restored and passes with the fix. No UI change, so there are no screenshots.

Checks

  • iOS and Android both covered, or the reason one is not — iOS only, verified by inspection. Android's MainScreenViewModel is @HiltViewModel resolved once per Home nav entry; profile events are SharedFlows collected in viewModelScope (cancelled with the view model), not handler arrays; and live observation jobs are cancelled explicitly in stopLiveObservations(). iOS now follows the same ownership model.
  • Screenshots for UI changes — no visual change.
  • Tests run — ProjectListHomeViewModelTests, ConnectionSessionCoordinatorTests, ActiveConnectionManagerProtocolTests, ServersSwitchTests on the iOS simulator (pass).

Risks

Home's model now lives for the app's life, so screen state (filters, dialogs) survives profile switches rather than resetting. ProjectListHomeView still copies the injected model into its own @State; that is safe while exactly one model is created.

Home's view model was built inside AppRootHomeHost.body, so each profile
switch (which re-renders the root) created another one. ProjectListHomeView
kept only the first; the rest were retained by the endless live-updates task
and left profile handlers on the shared coordinator. Every later switch made
all of them run a full Home refresh.

AppRootViewModel now owns one model for the app's life and passes it down. The
live-updates loop captures self weakly, and coordinator registrations are
returned as removable tokens that the model holds, so a discarded model both
deallocates and stops delivering.

Adds a deallocation regression test and keeps the save-counter registration
in ServersSwitchTests alive for its fixture.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review: no finding that 2+ of 3 passes agreed on.

1 finding(s) from a single pass, not posted
  • ios/OpenCode Mobile/OpenCode Mobile/Domain/Connection/ActiveConnectionManager.swift:526 medium: Registration-returning handler APIs silently drop callbacks when the token is not retained (deepseek-v4.1-flash)

opencode pr-review · passes: muse-spark-1.3-contributor, glm-5.3-flash, deepseek-v4.1-flash · $0.069 · 1 pass(es) stopped early

@ivanfenenko
ivanfenenko added this pull request to the merge queue Nov 19, 2026
Merged via the queue into main with commit 743ba71 Nov 19, 2026
@ivanfenenko
ivanfenenko deleted the fix/1088-home-view-model-leak branch November 19, 2026 14:29
ivanfenenko added a commit that referenced this pull request Nov 19, 2026
Follow-up to #1089, from an independent review.

- Handler removal releases the removed closures after the coordinator
  lock, so a handler whose deinit cancels another registration cannot
  deadlock.
- The registration token is nonisolated and removes entries from the
  lock-guarded handler storage, but the coordinator itself stays
  MainActor-inferred: profile-change work and handler delivery must stay
  on the main thread. A new test asserts a saved handler runs isolated
  to MainActor.
- Add a test that a profile save reuses the root's Home model; it is the
  invariant #1089 introduced and nothing covered it.
- Drop the unused Container.makeProjectListHomeViewModel factory and the
  200 ms sleep in the deallocation test.

This branch was previously deployed

2 inactive deployments
ios-release — cb0ade68 Deployed Nov 19, 2026 by ivanfenenko
android-release — cb0ade68 Deployed Nov 19, 2026 by ivanfenenko
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: switching between servers repeatedly gets progressively laggier

1 participant