From b17643c0295ce78d2d8cc581242dd21f1cdc88d3 Mon Sep 17 00:00:00 2001 From: "Shane B." Date: Mon, 23 Nov 2026 08:58:06 +0100 Subject: [PATCH 1/2] Sign out only after the local Anisette choice is saved clearUser() is built on RxDataStore.updateDataAsync, which starts its write when called rather than when subscribed. Passed straight to andThen, it cleared the session while the chain was being built, so accepting the upgrade signed the user out even if saving the choice failed. Defer it so it runs after the save completes. Found by lazzurs while working on #315. Co-Authored-By: Claude Opus 5.5 --- .../dev/wander/android/opentagviewer/MapsActivity.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/dev/wander/android/opentagviewer/MapsActivity.java b/app/src/main/java/dev/wander/android/opentagviewer/MapsActivity.java index 9417d93c..c08f6e3c 100644 --- a/app/src/main/java/dev/wander/android/opentagviewer/MapsActivity.java +++ b/app/src/main/java/dev/wander/android/opentagviewer/MapsActivity.java @@ -2144,13 +2144,19 @@ private void showDevicesAndFetch(final Observable asyncAppleService) { *

Accepting saves first and signs out only once the save has landed. The other order * looks the same right up until the write loses a race with the activity finishing, at * which point they sign in again and arrive back on the server they just left. + * + *

The sign-out is deferred because {@code clearUser()} starts writing when it is called, + * not when it is subscribed to: {@code RxDataStore.updateDataAsync} launches its coroutine + * straight away. Passed to {@code andThen} directly, it signed the user out at once, and + * still did if the save then failed. */ private void offerLocalAnisetteUpgradeIfDue() { this.runOnUiThread(() -> AnisetteUpgradeDialog.offerIfDue(this, this.userSettings, accepted -> { var saved = this.userSettingsRepo.storeUserSettings(this.userSettings); - var async = (accepted ? saved.andThen(this.userAuthRepo.clearUser()) : saved) + var signOut = Completable.defer(this.userAuthRepo::clearUser); + var async = (accepted ? saved.andThen(signOut) : saved) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(() -> { From 58ed7c3098c906097a145be7dd8aa33cb4211288 Mon Sep 17 00:00:00 2001 From: "Shane B." Date: Mon, 23 Nov 2026 09:02:25 +0100 Subject: [PATCH 2/2] Name the save-then-sign-out step, and test it on the JVM RxFlows.onlyAfter(first, () -> then) creates `then` only once `first` has completed. MapsActivity uses it for the local Anisette sign-out. The tests stand in for a DataStore write with one that writes when called, and fail with the old `first.andThen(then)`: checked by putting it back, and all three new tests went red. Co-Authored-By: Claude Opus 5.5 --- .../android/opentagviewer/MapsActivity.java | 10 ++-- .../opentagviewer/util/rx/RxFlows.java | 23 +++++++- .../opentagviewer/util/rx/RxFlowsTest.java | 53 +++++++++++++++++++ 3 files changed, 79 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/dev/wander/android/opentagviewer/MapsActivity.java b/app/src/main/java/dev/wander/android/opentagviewer/MapsActivity.java index c08f6e3c..d47f5370 100644 --- a/app/src/main/java/dev/wander/android/opentagviewer/MapsActivity.java +++ b/app/src/main/java/dev/wander/android/opentagviewer/MapsActivity.java @@ -2145,18 +2145,16 @@ private void showDevicesAndFetch(final Observable asyncAppleService) { * looks the same right up until the write loses a race with the activity finishing, at * which point they sign in again and arrive back on the server they just left. * - *

The sign-out is deferred because {@code clearUser()} starts writing when it is called, - * not when it is subscribed to: {@code RxDataStore.updateDataAsync} launches its coroutine - * straight away. Passed to {@code andThen} directly, it signed the user out at once, and - * still did if the save then failed. + *

{@link RxFlows#onlyAfter} and not {@code andThen}, because {@code clearUser()} starts + * writing when it is called. Passed to {@code andThen} directly it signed the user out at + * once, and still did if the save then failed. */ private void offerLocalAnisetteUpgradeIfDue() { this.runOnUiThread(() -> AnisetteUpgradeDialog.offerIfDue(this, this.userSettings, accepted -> { var saved = this.userSettingsRepo.storeUserSettings(this.userSettings); - var signOut = Completable.defer(this.userAuthRepo::clearUser); - var async = (accepted ? saved.andThen(signOut) : saved) + var async = (accepted ? RxFlows.onlyAfter(saved, this.userAuthRepo::clearUser) : saved) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(() -> { diff --git a/app/src/main/java/dev/wander/android/opentagviewer/util/rx/RxFlows.java b/app/src/main/java/dev/wander/android/opentagviewer/util/rx/RxFlows.java index 322cff5e..b0d13f3c 100644 --- a/app/src/main/java/dev/wander/android/opentagviewer/util/rx/RxFlows.java +++ b/app/src/main/java/dev/wander/android/opentagviewer/util/rx/RxFlows.java @@ -6,11 +6,12 @@ import io.reactivex.rxjava3.core.Observable; import io.reactivex.rxjava3.functions.BiConsumer; import io.reactivex.rxjava3.functions.Function; +import io.reactivex.rxjava3.functions.Supplier; /** * Stream compositions that the app depends on and that are easy to get subtly wrong. *
- * Both methods here exist because the composition, not the work, was the bug. They are kept + * Each method here exists because the composition, not the work, was the bug. They are kept * free of Android types so they can be tested on the JVM: the originals lived inside * {@code MapsActivity}, where nothing could reach them, and the failure they produced was a * missing card rather than an exception - no crash, no log, no test. @@ -45,6 +46,26 @@ public static Completable allThen(final Completable then, final Observable... return Completable.mergeArray(parts).andThen(then); } + /** + * Runs {@code first}, and only once it has completed, creates and runs {@code then}. + *
+ * {@code then} is taken as a supplier because some Completables here have already started + * by the time they are returned. Every DataStore write ({@code UserAuthRepository.clearUser}, + * {@code UserSettingsRepository.storeUserSettings} and the rest) is built on + * {@code RxDataStore.updateDataAsync}, which launches its coroutine when called. + * {@code first.andThen(repo.clearUser())} therefore signs the user out while the chain is + * being built, and still does if {@code first} fails. Nothing is logged and the success path + * looks identical, so it went unnoticed. + *
+ * If {@code first} fails, {@code then} is never created. + * + * @param first run first + * @param then called only after {@code first} completes + */ + public static Completable onlyAfter(final Completable first, final Supplier then) { + return first.andThen(Completable.defer(then)); + } + /** * Runs {@code work} for each item, one at a time and in order, carrying on if one fails. *
diff --git a/app/src/test/java/dev/wander/android/opentagviewer/util/rx/RxFlowsTest.java b/app/src/test/java/dev/wander/android/opentagviewer/util/rx/RxFlowsTest.java index 8be51670..5e163c64 100644 --- a/app/src/test/java/dev/wander/android/opentagviewer/util/rx/RxFlowsTest.java +++ b/app/src/test/java/dev/wander/android/opentagviewer/util/rx/RxFlowsTest.java @@ -330,4 +330,57 @@ public void oneAtATime_completesImmediatelyForAnEmptyList() { assertEquals(List.of("0/0"), progress); } + + // ------------------------------------------------------------------------------------- + // onlyAfter - a DataStore write that must wait for something before it + // ------------------------------------------------------------------------------------- + + /** + * Stands in for a repository method built on {@code RxDataStore.updateDataAsync}: the write + * happens when the method is called, and the Completable it returns only reports on it. + */ + private static Completable eagerWrite(final AtomicInteger writes) { + writes.incrementAndGet(); + return Completable.complete(); + } + + @Test + public void onlyAfter_doesNotStartTheNextStepBeforeTheFirstCompletes() { + final AtomicInteger signOuts = new AtomicInteger(); + final PublishSubject save = PublishSubject.create(); + + final TestObserver result = RxFlows.onlyAfter( + save.ignoreElements(), () -> eagerWrite(signOuts)).test(); + + assertEquals("signed out before the save landed", 0, signOuts.get()); + + save.onComplete(); + + result.assertComplete(); + assertEquals(1, signOuts.get()); + } + + /** The bug the helper exists for: a failed save must leave the user signed in. */ + @Test + public void onlyAfter_neverStartsTheNextStepWhenTheFirstFails() { + final AtomicInteger signOuts = new AtomicInteger(); + + RxFlows.onlyAfter(Completable.error(new RuntimeException("save failed")), + () -> eagerWrite(signOuts)) + .test() + .assertError(RuntimeException.class); + + assertEquals(0, signOuts.get()); + } + + /** Building the chain without subscribing must not do the second step either. */ + @Test + public void onlyAfter_doesNothingUntilSubscribed() { + final AtomicInteger signOuts = new AtomicInteger(); + + final Completable unused = RxFlows.onlyAfter(Completable.complete(), + () -> eagerWrite(signOuts)); + + assertEquals(0, signOuts.get()); + } }