Skip to content

Run the account's loop on its own thread so iCloud calls stop queueing behind fetches - #273

Merged
parawanderer merged 8 commits into
parawanderer:mainfrom
lazzurs:fix/126-drive-the-account-loop-from-its-own-thread
Nov 21, 2026
Merged

parawanderer merged 8 commits into
parawanderer:mainfrom
lazzurs:fix/126-drive-the-account-loop-from-its-own-thread

Conversation

@lazzurs

@lazzurs lazzurs commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

Fixes #126.

Problem

Every call into Python held PythonLock, because FindMy.py's sync AppleAccount enters its event loop per call with run_until_complete. A first import walks thousands of key indices inside a single fetch_location, so opening the iCloud screen during one meant waiting minutes.

Change

  • app/src/main/python/account_loop.py (new): attach(account) replaces _evt_loop with a stand-in that submits coroutines to the real loop, which runs forever on its own thread. Same loop, session and identity.
  • The account is still used by one operation at a time. request_pet (called when a keychain session opens) swaps the login state across a GSA round trip, so a report request running beside it would read a missing dsid. Ordinary calls hold the account's turn for their whole length; location fetches run inside interleaving() and take the turn once per fetch_raw_reports. An iCloud step therefore waits for at most one request.
  • main.py attaches on sign-in and restore, detaches in closeAccount, and wraps the two fetch call sites in interleaving(). An account dropped without being closed stops its thread via a finaliser.
  • attach degrades rather than raising: if the pinned FindMy.py ever stops exposing fetch_raw_reports or _evt_loop, the account still gets its thread (fetches then take whole turns) or is left as it was.
  • icloud_bridge.openSession refuses an account whose loop is not attached. The iCloud test double and OpeningARealICloudSessionTest attach their accounts.
  • PythonICloudService and FetchFromICloudActivity no longer take PythonLock. Location fetches still take it against each other.
  • Removed icloud_loading_waiting_for_location_check from all locales with scripts/add_strings.py --remove; that wait no longer exists.
  • AGENTS.md rule 21 documents the constraint.

Testing

🤖 Generated with Claude Code

Rob Lazzurs and others added 3 commits October 16, 2026 21:31
…g behind fetches

Every call into Python was serialised behind PythonLock because FindMy.py's sync
AppleAccount enters its event loop per call with run_until_complete. A first import
walks thousands of key indices inside a single fetch_location, so the iCloud screen
waited minutes for its turn (parawanderer#126).

account_loop.attach now swaps the account's _evt_loop for a stand-in that hands
coroutines to the real loop, running forever on a thread of its own. Same loop,
same session, same identity.

The account is still used by one operation at a time: request_pet swaps the login
state out across a GSA round trip, so a report request running beside it would read
a missing dsid. Ordinary calls take the account's turn for their whole length; a
location fetch inside interleaving() takes it once per fetch_raw_reports, so an
iCloud step waits for one request rather than the whole walk.

PythonICloudService no longer takes PythonLock. Location fetches still do, against
each other. The "waiting for the location check" caption is gone with the wait it
described.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A failed sign-in or an abandoned code screen never reaches closeAccount. That used
to cost two sockets until the collector ran; with the loop on its own thread it
would also leave a thread idling forever. A finaliser on the account now lets
whatever is already on the loop finish - including the close FindMy.py's own
finaliser schedules - and then stops the thread.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…aise

The emulator suite's two iCloud bridge classes built accounts with a bare event
loop, which openSession now refuses. They attach it the way main does.

attach no longer assumes fetch_raw_reports exists or that the account can be
weakly referenced. Without the method the loop still gets its thread and a fetch
inside interleaving() takes one whole turn, as before; an exception there would
fail the restore and sign the user out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@parawanderer

Copy link
Copy Markdown
Owner

Sidenote that I will review this (and the other PRs on this repository) once we're through with #274, since that's what is blocking the app for most people. As well as blocking login-and-test cycles of the app in general.

Thanks already for looking into and fixing this issue though!

@parawanderer parawanderer self-assigned this Oct 26, 2026
@parawanderer
parawanderer self-requested a review October 26, 2026 09:50
parawanderer and others added 5 commits November 21, 2026 13:53
…number this one 22

main.py: parawanderer#274 removed nothing near the attach, so it is kept. Both
places that create an account (sign-in and restore) still attach.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Seen on the emulator: MapsActivity restored a fresh account and closed
the old one while the background account read was resuming on the old
one. account_loop raised "This account has been closed", resume reported
that as MEMBERSHIP_UNUSABLE, and the refresher forgot the membership, so
the next fetch asked for a device passcode again.

Before this PR the iCloud service and the account swap both held
PythonLock, so the two never overlapped. Now account_loop raises its own
AccountClosedError, and resume reports it through the ordinary
classifier: the background read logs it and tries again next time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Leaving Settings after a theme change rebuilds MapsActivity. The new one
restores a fresh account and closes the old one under PythonLock. The
old screen's fetch, queued on that lock, then ran on the closed account
("HttpSession has been closed"). Its subscription was already disposed,
so the error had nowhere to go, and RxJava's default handler killed the
process.

- PythonAppleService reads the account once it holds the lock: the
  current one if it was replaced meanwhile, nothing after a sign-out.
  HistoryViewActivity keeps an instance too, so this covers it as well.
- An UndeliverableException is logged instead of crashing. Anything else
  reaching RxJavaPlugins is a bug and still crashes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@parawanderer

Copy link
Copy Markdown
Owner

Tested on an emulator and pushed four commits on top:

On the emulator, on an emulated-Mac sign-in: the iCloud tag list loads straight away during a fetch instead of waiting for it, and repeated theme changes no longer lose the membership or crash. Python suite: 346 passed.

Drafted by Claude Code

@parawanderer

Copy link
Copy Markdown
Owner

@lazzurs Thanks for this one too - another annoying issue that I didn't expect somebody to actually bother helping with here. Unblocking this makes the usage experience of the app much better. Question from the other thread still stands

Merging this in any case, it'll be in the next release with all the other fixes.

@parawanderer
parawanderer merged commit e21edde into parawanderer:main Nov 21, 2026
4 checks passed

This branch was previously deployed

1 inactive deployment
Android Build — 2e16f872 Deployed Nov 21, 2026 by parawanderer via build #377
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.

iCloud calls queue behind a location fetch, because the shared loop is driven synchronously

2 participants