diff --git a/app/src/main/python/icloud_bridge.py b/app/src/main/python/icloud_bridge.py index 7b175ecf..4e698f95 100644 --- a/app/src/main/python/icloud_bridge.py +++ b/app/src/main/python/icloud_bridge.py @@ -242,6 +242,26 @@ def _unexpected(what: str) -> str: ) +def _saysNothingAboutTheMembership(error: BaseException) -> bool: + """ + Whether a failed `resume` happened around the stored membership rather than to it. + + Matched by type, like `main.classifyLoginFailure`'s network check, and aiohttp by module so + this needs no import of it. Anything else stays MEMBERSHIP_UNUSABLE, because a removed peer + arrives as a `KeychainSessionError` or a decryption failure and nothing narrower. + """ + import asyncio + + if isinstance(error, (account_loop.AccountClosedError, AppleServiceUnavailableError)): + return True + if _needsAFreshSignIn(error): + return True + if isinstance(error, (TimeoutError, asyncio.TimeoutError, OSError)): + return True + module = type(error).__module__ or "" + return module.startswith("aiohttp") or module.startswith("aiohappyeyeballs") + + def _lastLineOf(detail: str) -> str: return detail.strip().splitlines()[-1] if detail.strip() else "" @@ -665,12 +685,15 @@ def resume(self, peerJson: str) -> str: print(f"iCloud bridge: reading as {peer.peer_id}, with no passcode") return json.dumps({"ok": True}) - except account_loop.AccountClosedError: - # Not the membership: the account under this session was replaced and closed. Both - # Java callers forget the membership on MEMBERSHIP_UNUSABLE, which here cost a device - # passcode for nothing. The ordinary classification lets the background read retry. - return _unexpected("resuming the keychain session") - except Exception: + except Exception as e: + # **Both Java callers forget the membership on MEMBERSHIP_UNUSABLE**, and joining + # again costs a device passcode. So only a failure of the membership itself gets it. + # A closed account, a refused sign-in, Apple declining and the network going away + # say nothing about the peer, and go through the ordinary classification: sign in + # again, wait, or retry. The closed account was seen on a device on 2026-11-21. + if _saysNothingAboutTheMembership(e): + return _unexpected("resuming the keychain session") + # Worth its own reason. A membership that no longer works is not a broken app - the # peer may have been removed from the account - and the answer is to unlock with a # passcode again, not to retry this. diff --git a/app/src/test/python/test_icloud_bridge.py b/app/src/test/python/test_icloud_bridge.py index e6731cd2..2b128a31 100644 --- a/app/src/test/python/test_icloud_bridge.py +++ b/app/src/test/python/test_icloud_bridge.py @@ -954,6 +954,44 @@ def test_a_closed_account_is_not_a_membership_that_stopped_working(self, session assert answer["reason"] != icloud_bridge.REASON_MEMBERSHIP_UNUSABLE assert answer["reason"] == icloud_bridge.REASON_UNKNOWN + @pytest.mark.parametrize( + ("error", "reason"), + [ + (InvalidCredentialsError("refused"), icloud_bridge.REASON_CREDENTIALS_REJECTED), + (None, icloud_bridge.REASON_APPLE_DECLINED), + (TimeoutError(), icloud_bridge.REASON_UNKNOWN), + (ConnectionResetError("reset by peer"), icloud_bridge.REASON_UNKNOWN), + ], + ids=["credentials rejected", "apple 429", "timeout", "connection reset"], + ) + def test_a_failure_around_the_membership_does_not_forget_it(self, session, error, reason): + """ + Both Java callers forget the stored membership on MEMBERSHIP_UNUSABLE, and joining again + costs a device passcode. A refused sign-in means signing in again (rule 15), and Apple + declining or the network dropping means waiting. None of them says the peer is gone. + """ + from findmy.errors import AppleServiceUnavailableError # noqa: PLC0415 + + made = session(FakeClient()) + made.client._resumeError = ( + error if error is not None + else AppleServiceUnavailableError(429, "pcs_keys", None)) + + answer = json.loads(made.resume(json.dumps(aStoredMembership()))) + + assert not answer["ok"] + assert answer["reason"] == reason + + def test_a_refused_keychain_read_still_means_the_membership_is_unusable(self, session): + """The other half: the shape a removed peer arrives in keeps its own reason.""" + made = session(FakeClient()) + made.client._resumeError = icloud_bridge.KeychainSessionError( + "No keys were recovered for the 'Manatee' view") + + answer = json.loads(made.resume(json.dumps(aStoredMembership()))) + + assert answer["reason"] == icloud_bridge.REASON_MEMBERSHIP_UNUSABLE + def test_rubbish_in_storage_does_not_crash(self, session): made = session(FakeClient())