From 2cb630ed6dcaf1edb269c428d2c1c42367d70a8a Mon Sep 17 00:00:00 2001 From: Shane B Date: Sat, 21 Nov 2026 14:16:00 +0100 Subject: [PATCH] Don't crash when a fetch outlives the map screen that started it 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 --- .../opentagviewer/OpenAirTagApplication.java | 5 ++ .../python/PythonAppleService.java | 36 +++++++++- .../util/rx/UndeliverableErrors.java | 47 ++++++++++++ .../util/rx/UndeliverableErrorsTest.java | 71 +++++++++++++++++++ 4 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/dev/wander/android/opentagviewer/util/rx/UndeliverableErrors.java create mode 100644 app/src/test/java/dev/wander/android/opentagviewer/util/rx/UndeliverableErrorsTest.java diff --git a/app/src/main/java/dev/wander/android/opentagviewer/OpenAirTagApplication.java b/app/src/main/java/dev/wander/android/opentagviewer/OpenAirTagApplication.java index 9110d021..d43d3ac4 100644 --- a/app/src/main/java/dev/wander/android/opentagviewer/OpenAirTagApplication.java +++ b/app/src/main/java/dev/wander/android/opentagviewer/OpenAirTagApplication.java @@ -18,6 +18,8 @@ import dev.wander.android.opentagviewer.db.repo.model.UserSettings; import dev.wander.android.opentagviewer.db.room.OpenTagViewerDatabase; import dev.wander.android.opentagviewer.service.NearbyScanService; +import dev.wander.android.opentagviewer.util.rx.UndeliverableErrors; +import io.reactivex.rxjava3.plugins.RxJavaPlugins; import io.reactivex.rxjava3.schedulers.Schedulers; import java.io.FileInputStream; @@ -41,6 +43,9 @@ public void onCreate() { return; } + // A fetch that fails after its screen was rebuilt must not take the app down with it. + RxJavaPlugins.setErrorHandler(UndeliverableErrors.handler()); + // What PyApplication did, which this used to extend. Done by hand so the line above can // come first. Python.start(new AndroidPlatform(this)); diff --git a/app/src/main/java/dev/wander/android/opentagviewer/python/PythonAppleService.java b/app/src/main/java/dev/wander/android/opentagviewer/python/PythonAppleService.java index 6ce38090..6bf8500e 100644 --- a/app/src/main/java/dev/wander/android/opentagviewer/python/PythonAppleService.java +++ b/app/src/main/java/dev/wander/android/opentagviewer/python/PythonAppleService.java @@ -117,6 +117,28 @@ public PythonAppleAccount getAccount() { return this.account; } + /** + * The account to fetch with, read once the caller holds {@link PythonLock}. + * + *

Not necessarily this service's own. A theme or language change rebuilds + * {@code MapsActivity}, which restores a fresh account and closes this one in {@link #setup}, + * under the lock. A fetch queued on the lock behind that swap then ran against the closed + * account and failed. The fetch is still worth making, so it goes to whichever account is + * signed in now. Null after a sign-out. + */ + private PythonAppleAccount accountToFetchWith() { + final PythonAppleService now = INSTANCE; + if (now == this) { + return this.account; + } + if (now == null) { + Log.i(TAG, "Signed out while this fetch was waiting; skipping it"); + return null; + } + Log.i(TAG, "The account was replaced while this fetch was waiting; using the new one"); + return now.account; + } + public Observable getLastReports(final List requests, final int hoursToGoBack) { return Observable.fromCallable(() -> { if (requests.isEmpty()) { @@ -124,12 +146,17 @@ public Observable getLastReports(final List reque } return PythonLock.holding(() -> { + final PythonAppleAccount fetchWith = this.accountToFetchWith(); + if (fetchWith == null) { + return emptyResult(); + } + var py = Python.getInstance(); var module = py.getModule(MODULE_MAIN); var returned = module.callAttr( "getLastReports", - new Kwarg("account", this.account.getAccountObj()), + new Kwarg("account", fetchWith.getAccountObj()), new Kwarg("idToAccessoryData", requests), new Kwarg("hoursBack", hoursToGoBack) ); @@ -151,12 +178,17 @@ public Observable getReportsBetween(final List re } return PythonLock.holding(() -> { + final PythonAppleAccount fetchWith = this.accountToFetchWith(); + if (fetchWith == null) { + return emptyResult(); + } + var py = Python.getInstance(); var module = py.getModule(MODULE_MAIN); var returned = module.callAttr( "getReports", - new Kwarg("account", this.account.getAccountObj()), + new Kwarg("account", fetchWith.getAccountObj()), new Kwarg("idToAccessoryData", requests), new Kwarg("unixStartMs", startTimeUnixMS), new Kwarg("unixEndMs", endTimeUnixMS) diff --git a/app/src/main/java/dev/wander/android/opentagviewer/util/rx/UndeliverableErrors.java b/app/src/main/java/dev/wander/android/opentagviewer/util/rx/UndeliverableErrors.java new file mode 100644 index 00000000..bca749d5 --- /dev/null +++ b/app/src/main/java/dev/wander/android/opentagviewer/util/rx/UndeliverableErrors.java @@ -0,0 +1,47 @@ +package dev.wander.android.opentagviewer.util.rx; + +import android.util.Log; + +import io.reactivex.rxjava3.exceptions.UndeliverableException; +import io.reactivex.rxjava3.functions.Consumer; + +/** + * What happens to an error RxJava has nobody to give to. + * + *

RxJava's default is to hand it to the thread's uncaught-exception handler, which on Android + * kills the process. The common way to reach that here is a screen that went away while its + * work was still running: a theme or language change rebuilds {@code MapsActivity}, the old one + * disposes its subscriptions, and its location fetch, already queued on {@code PythonLock}, + * then fails against an account the new screen has just closed. Nobody is listening, nothing + * was lost, and the new screen fetches for itself. That crashed the app on 2026-11-21. + * + *

Only {@link UndeliverableException} is swallowed. That is the wrapper RxJava uses for + * exactly this case, an error with no subscriber left. Anything else reaching the handler is a + * bug, such as an exception thrown inside an {@code onError} consumer, and still crashes, so it + * gets reported rather than hidden. + */ +public final class UndeliverableErrors { + private static final String TAG = UndeliverableErrors.class.getSimpleName(); + + private UndeliverableErrors() { + } + + /** For {@code RxJavaPlugins.setErrorHandler}. */ + public static Consumer handler() { + return UndeliverableErrors::handle; + } + + static void handle(final Throwable error) { + if (error instanceof UndeliverableException) { + Log.w(TAG, "An error arrived after whoever asked had gone; ignoring it", + error.getCause() != null ? error.getCause() : error); + return; + } + + final Thread thread = Thread.currentThread(); + final Thread.UncaughtExceptionHandler crash = thread.getUncaughtExceptionHandler(); + if (crash != null) { + crash.uncaughtException(thread, error); + } + } +} diff --git a/app/src/test/java/dev/wander/android/opentagviewer/util/rx/UndeliverableErrorsTest.java b/app/src/test/java/dev/wander/android/opentagviewer/util/rx/UndeliverableErrorsTest.java new file mode 100644 index 00000000..2737b26b --- /dev/null +++ b/app/src/test/java/dev/wander/android/opentagviewer/util/rx/UndeliverableErrorsTest.java @@ -0,0 +1,71 @@ +package dev.wander.android.opentagviewer.util.rx; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.concurrent.atomic.AtomicReference; + +import io.reactivex.rxjava3.core.Observable; +import io.reactivex.rxjava3.observers.DisposableObserver; +import io.reactivex.rxjava3.plugins.RxJavaPlugins; + +public class UndeliverableErrorsTest { + private final AtomicReference crashed = new AtomicReference<>(); + private Thread.UncaughtExceptionHandler previous; + + @Before + public void catchCrashes() { + this.previous = Thread.currentThread().getUncaughtExceptionHandler(); + Thread.currentThread().setUncaughtExceptionHandler((t, e) -> this.crashed.set(e)); + RxJavaPlugins.setErrorHandler(UndeliverableErrors.handler()); + } + + @After + public void putThemBack() { + RxJavaPlugins.reset(); + Thread.currentThread().setUncaughtExceptionHandler(this.previous); + } + + /** + * The 2026-11-21 crash: the screen was rebuilt, disposing the fetch's subscription while the + * fetch was running, and the fetch then failed. A checked exception, as the real + * {@code PythonAppleFindMyException} is. + */ + @Test + public void aFetchThatFailsAfterItsScreenWentAwayDoesNotCrash() { + final DisposableObserver screen = new DisposableObserver<>() { + @Override + public void onNext(final Object o) { + } + + @Override + public void onError(final Throwable e) { + } + + @Override + public void onComplete() { + } + }; + + Observable.fromCallable(() -> { + screen.dispose(); + throw new Exception("Error while retrieving last reports for account via python!"); + }).subscribe(screen); + + assertNull("an error nobody is listening for must not crash the app", this.crashed.get()); + } + + /** RxJava passes these through unwrapped because they mean a bug; those must still crash. */ + @Test + public void aBugStillCrashes() { + final IllegalStateException bug = new IllegalStateException("a real bug"); + + RxJavaPlugins.onError(bug); + + assertSame(bug, this.crashed.get()); + } +}