Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,46 @@ public PythonAppleAccount getAccount() {
return this.account;
}

/**
* The account to fetch with, read once the caller holds {@link PythonLock}.
*
* <p><b>Not necessarily this service's own.</b> 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<FetchResult> getLastReports(final List<AccessoryRequest> requests, final int hoursToGoBack) {
return Observable.fromCallable(() -> {
if (requests.isEmpty()) {
return emptyResult();
}

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)
);
Expand All @@ -151,12 +178,17 @@ public Observable<FetchResult> getReportsBetween(final List<AccessoryRequest> 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>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.
*
* <p><b>Only {@link UndeliverableException} is swallowed.</b> 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<Throwable> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Throwable> 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<Object> 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());
}
}