Skip to content

Android ANR: SessionFamily.resolveRoot/memberIds on main thread (1.4.0 (37)) #981

Closed

Description

@ivanfenenko

Summary

New ANR cluster on the newest Android release 1.4.0 (37): the main thread is stuck in SessionFamily.memberIds / SessionFamily.resolveRoot while the Home list recomputes rows from live data.

Crashlytics issue Events Users Top frame
27876e6c003b436dd89938681d30dabe 10 1 SessionFamily.resolveRoot (SessionFamily.java:218, mutableSetOf)
b0f05655cbed44b8cf2dad1ea5d36054 7 1 SessionFamily.memberIds (SessionFamily.java:434, HashMap.put)
79f77c0a4aa13eb4f4f0060209e5efee 1 1 SessionFamily.resolveRoot (LinkedHashSet.<init>)
  • Total: 18 events, 1 user, all on 1.4.0 (37). Build 37 has 138 sessions so far (Nov 3–4).
  • Device: realme RMX3741, Android 15 (API 35). Both foreground and background.
  • Window: Nov 4 2026, 18:46–19:12 CET.
  • First seen today; none of these showed up on older builds.

Stack path

MainScreenViewModel.startLiveObservations combine{...}.collect   (main thread)
 -> MainScreenViewModel.updateSectionFromLiveData
 -> MainScreenViewModel.homeSummaries
 -> SessionActivitySnapshot.homeActivitySessionId
 -> SessionFamily.memberIds
 -> SessionFamily.resolveRoot -> mutableSetOf / HashMap.put

Root-cause hypothesis (code on main @ 2803d3d)

The Home row family lookup is quadratic and runs on the main thread for every live emission:

  1. MainScreenViewModel.kt#L1512-L1528: startLiveObservations runs combine(observeSessions, observeActivity, observeReadyIds).collect in viewModelScope.launch, which defaults to Dispatchers.Main. Each session or status event (streaming status updates come in very often) calls updateSectionFromLiveData on the main thread.
  2. MainScreenViewModel.kt#L1805-L1818: homeSummaries calls snapshot.homeActivitySessionId(session.id, family) once per root session.
  3. SessionModels.kt#L174: homeActivitySessionId calls SessionFamily.memberIds(rootId, sessions) again for each root.
  4. SessionModels.kt#L199-L210: memberIds rebuilds parentById = sessions.associate {...} (O(N) map) for each root. It then calls resolveRoot for every session.
  5. SessionModels.kt#L213-L224: resolveRoot allocates a new mutableSetOf and walks the parent chain each time.

The cost per emission is O(roots × sessions × depth), with about roots × sessions short-lived set and map allocations. A worktree with many sessions and sub-agent children makes this heavy work on the UI thread, and it repeats on every status tick. The ANR frames sit in HashMap.put / LinkedHashSet.<init>, which fits allocation and GC pressure, not an infinite loop (seen already guards cycles).

Suggested fix

  • Build parentById and a rootOf map once per homeSummaries call, memoizing resolved roots. Group the members by root so each root's lookup is O(1) and total cost is O(N).
  • Move homeSummaries off the main thread (flowOn(Dispatchers.Default) / withContext(Default)). Consider conflate()/debounce on the combined live flow.
  • Add a perf test in SessionFamilyActivityTest with something like 500 sessions and deep chains.

Labels: crash triage, Crashlytics project opencode-mobile-release.

Activity

  1. added
    bug
    android
    priority: high
    crash
    on Nov 4, 2026
  2. added 2 commits that reference this issue on Nov 5, 2026
  3. ivanfenenko commented on Nov 6, 2026

    @ivanfenenko
    CollaboratorAuthor

    Crashlytics triage, 2026-11-06: fix PR #984 (8adeb6112a, merged 2026-11-05 10:18 UTC) closed this issue, and it is now in a release build.

    • Android Release run 38, 1.4.1 (38), commit a6e744cd65 (Bump app versions: Android 1.4.1, iOS 1.3.1 #986), built 2026-11-05 11:25 CET: https://github.com/opencode-mobile/opencode-mobile/actions/runs/42010644249 . Compare shows a6e744cd65 ahead of 8adeb6112a.
    • Crashlytics shows no sessions on build 38 yet. The ANRs are all on 1.4.0 (37): 27876e6c (11 events / 1 user), b0f05655 (8 / 1) and 79f77c0a (1 / 1), all from the same realme RMX3741 user. Build 37 has 400 sessions; no new events on these signatures since yesterday's count.
    • I'll close-confirm once build 38 has been out 7+ days with ~50+ sessions and 0 events for 27876e6c, b0f05655 and 79f77c0a.
    • Related, watch only: a new ANR 3f7e7e9b (AndroidComposeView_androidKt.C, 1 event / 1 user on 1.4.0 (37)). It's below the filing threshold.
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

    androidbugcrashpriority: high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions