Repository navigation
fix(player): stop MediaController listener race from swallowing real playback failures (#642) - #649
Conversation
…playback failures Confirmed on-device via adb logcat (issue #642 — "Playback broken - regression") that PlayerViewModel had two compounding bugs, both firing on essentially every Player-screen open, not just the vault-audio-unification work landed the same day: 1. `playerListener` was declared textually *after* `init{}` in the class body. `init{}` calls connectController(), which can invoke connectWithRetry()'s callback synchronously (MoreExecutors.directExecutor() + an already-resolved singleton controllerFuture — true on essentially every open after the app's first ever MediaController connection this session). At that point `playerListener` hadn't been assigned yet, so `controller.addListener(null)` threw — logged verbatim on-device as "listener must not be null". The retry silently recovered ~500ms later, which is presumably why this went unnoticed. 2. That forced retry opened a real race: loadItem()'s completion tail and connectWithRetry()'s success tail could each independently run their own "reattach or play" bootstrap for the same freshly-opened item, bumping playGeneration twice instead of once (confirmed on-device: "gen 2 < current 2"). Every bump makes onIsPlayingChanged unconditionally treat the *next* `false` event as a stale ExoPlayer transition blip and swallow it — an assumption that breaks when there are two real triggers stacked up. A swallowed event can be the genuine "playback never actually started" signal, leaving PlayerUiState.isPlaying stuck at the optimistic `true` set by play() with togglePlayPause() re-deriving the same `true` on every tap — exactly the reported symptom (shows as playing, no sound, button doesn't respond). Fixes: - Move playerListener's declaration above init{} so it's always initialized before connectController() can possibly reach it. - Consolidate loadItem()'s and connectWithRetry()'s "reattach or play" tails into one attachOrPlay() function, guarded by hasAttachedOrPlayed so only one of the two triggers can actually run it per ViewModel instance. Neither bug was caught by the existing Robolectric suite: mockk's relaxed MediaController mock has no real implementation behind addListener() to validate a non-null contract, so it silently accepts null. New regression tests capture the actual listener argument (would NPE on a null capture) and call attachOrPlay() a second time directly (attachOrPlay made internal for this, matching LibravaultLogger's existing "internal for direct testability" convention) to assert the guard holds — the real async race isn't reproducible through this module's synchronous UnconfinedTestDispatcher harness. Closes #642. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
This PR was opened outside the automated pipeline (author isn't |
📱 iOS Build Status: PASSED⏭️ iOS Simulator Build & Unit Tests - skipped iOS UI tests run in a separate, informational job and are not reflected above — check "iOS UI Tests (informational)" for those. |
Principal review — verdict:
|
Summary
Fixes issue #642 ("Playback broken - regression"). Root-caused on-device via
adb logcat, not just static reading — see the diagnosis comment on #642 for the full trace.Two compounding bugs in
PlayerViewModel'sMediaControllerconnection lifecycle, both firing on essentially every Player-screen open:playerListenerreferenced before it's initialized. It was declared textually afterinit{}in the class body.init{}callsconnectController(), which can invokeconnectWithRetry()'s callback synchronously (MoreExecutors.directExecutor()+ an already-resolved singletoncontrollerFuture— true on essentially every open after the app's first-everMediaControllerconnection this session, sinceLibraryViewModelalso resolves it on every Library visit). At that pointplayerListenerhadn't been assigned yet, socontroller.addListener(null)threw — logged on-device verbatim as"listener must not be null". The retry recovered ~500ms later, which is presumably why this went unnoticed until it compounded with bug 2.The forced retry created a real double-trigger race.
loadItem()'s completion tail andconnectWithRetry()'s success tail could each independently run their own "reattach or play" bootstrap for the same freshly-opened item, bumpingplayGenerationtwice instead of once (confirmed on-device:"Ignoring stale onIsPlayingChanged(false) — gen 2 < current 2"). Every bump makesonIsPlayingChangedunconditionally treat the nextfalseevent as a stale ExoPlayer transition blip and swallow it — an assumption that breaks under a genuine double-trigger. A swallowed event can be the real "playback never actually started" signal, leavingPlayerUiState.isPlayingstuck at the optimistictrueset byplay(), withtogglePlayPause()re-deriving the sametrueon every tap (reads the realctrl.isPlaying, which isfalse, flips to!false = true— visually a no-op). That's the reported symptom: shows as playing, no sound, play/pause button doesn't respond.Fix
playerListener's declaration aboveinit{}.loadItem()'s andconnectWithRetry()'s "reattach or play" tails into oneattachOrPlay()function, guarded byhasAttachedOrPlayedso only one of the two call sites can actually run it per ViewModel instance.Why the existing suite (87 tests) never caught this
Both bugs depend on real
ListenableFuture/dispatcher timing Robolectric-free mock tests don't reproduce, and specifically for bug 1:mockController(mockk, relaxed) has no real implementation behindaddListener()to validate Media3's actual non-null contract, so it silently acceptsnull.Tests added
playerListener is fully initialized before it's attached to the controller— captures the actual listener argument passed toaddListener()and invokes a method on it; a null capture would NPE at the call site regardless of the argument value, which is what makes this a real regression guard for bug 1.attachOrPlay only bootstraps playback once per ViewModel instance— callsattachOrPlay()(madeinternal, matchingLibravaultLogger's existing documented "internal for direct testability" convention, since the real async race isn't reproducible through this module's synchronousUnconfinedTestDispatcherharness) a second time directly and asserts it's a no-op.Full existing suite re-run clean:
feature:player(14/14, incl. the 2 new),feature:library,feature:readerall pass.Not in scope
The Settings-screen freeze/rendering-artifact also reported alongside this issue is a separate, not-yet-root-caused problem — nothing Settings-related showed up in the logcat capture's window. Tracked as an open follow-up on #642, not fixed here.
🤖 Generated with Claude Code
Co-Authored-By: Claude Sonnet 5 noreply@anthropic.com