[Bug] Service shutdown can detach playback before final session sync snapshots progress #46

Closed
opened 2026-09-18 08:48:43 +02:00 by alexander · 0 comments
Owner

Migrated from GitHub issue #142. Forgejo issue: #46. GitHub source content synchronized on 2026-09-19. Migrated issue references below use Forgejo numbers where an equivalent issue exists; historical references without a Forgejo counterpart remain explicit GitHub links.

Classification

Playback/session lifecycle correctness follow-up discovered while investigating #43 / GitHub PR #140. Keep this separate from the cold-resumption fix; it is a teardown ordering defect, not a reason to add another resume policy.

Current defect

PlaybackService.onDestroy() currently orders teardown as:

  1. flushProgress() — launches the local progress record asynchronously on the application scope;
  2. sessionSync.onShutdown() — itself launches asynchronous shutdown work;
  3. immediately sessionSync.attach(null);
  4. later releases/detaches the remaining player/session owners.

SessionSyncCoordinator.onShutdown() eventually closes the current session with a ServiceShutdown reason. That close needs a player snapshot. Because attach(null) is not ordered behind completion of the asynchronous shutdown close, the coordinator can observe no player and skip the final session/progress close.

This is especially relevant during process/service replacement or an app update, where the last server-acknowledged progress may otherwise lag what the listener actually heard.

Expected behavior

Service teardown must capture the final playback/session snapshot while the player required to produce it is still attached, and must order final local/server durability work explicitly before detaching that dependency.

The fix must not rely on onDestroy() as the only durability mechanism for abrupt process death; existing periodic/local durability remains necessary.

Investigation / implementation scope

  • Define the shutdown ordering contract between PlaybackService and SessionSyncCoordinator.
  • Add a direct SessionSyncCoordinator.onShutdown() regression test.
  • Prove the final snapshot is captured before player detachment.
  • Decide whether shutdown should synchronously capture an immutable snapshot first and hand that to asynchronous persistence/close work, or use another narrowly ordered mechanism.
  • Preserve cancellation semantics and avoid blocking Android service teardown on unbounded network work.
  • Keep profile/book ownership with the captured snapshot; do not re-resolve the active profile after teardown starts.
  • Keep logs redaction-safe.

Out of scope

  • Do not redesign resume freshness; GitHub PR #93 / ResumeFreshnessCoordinator remains the position-decision owner.
  • Do not fold this lifecycle repair into GitHub PR #140 unless a later proof shows it is required for that narrow cold-resumption safety fix.
  • Do not add Android-Auto-specific behavior here; #45 tracks the separate cold Android Auto surfacing symptom.

Acceptance criteria

  • A deterministic test fails with the current onShutdown() -> immediate attach(null) ordering and passes after the repair.
  • Final progress/session close uses the last valid player snapshot even when teardown continues immediately afterward.
  • Detaching/releasing the player cannot race the shutdown close into a no-op.
  • Normal Pause/seek/session-transition synchronization behavior remains unchanged.
  • Physical/process-replacement testing verifies progress does not regress across an adb install -r update where practical.
  • #43 — cold headset/background Play can resume the correct book at 0:00.
  • GitHub PR #140 — narrow cold-resumption freshness validation; intentionally does not redesign teardown.
  • #45 — separate Android Auto cold/background surfacing symptom.
> Migrated from [GitHub issue #142](https://github.com/AlexanderWilhelmsenBerg/Audiobookshelf-Manager-Claude/issues/142). Forgejo issue: #46. GitHub source content synchronized on 2026-09-19. Migrated issue references below use Forgejo numbers where an equivalent issue exists; historical references without a Forgejo counterpart remain explicit GitHub links. ## Classification Playback/session lifecycle correctness follow-up discovered while investigating #43 / [GitHub PR #140](https://github.com/AlexanderWilhelmsenBerg/Audiobookshelf-Manager-Claude/pull/140). Keep this separate from the cold-resumption fix; it is a teardown ordering defect, not a reason to add another resume policy. ## Current defect `PlaybackService.onDestroy()` currently orders teardown as: 1. `flushProgress()` — launches the local progress record asynchronously on the application scope; 2. `sessionSync.onShutdown()` — itself launches asynchronous shutdown work; 3. immediately `sessionSync.attach(null)`; 4. later releases/detaches the remaining player/session owners. `SessionSyncCoordinator.onShutdown()` eventually closes the current session with a `ServiceShutdown` reason. That close needs a player snapshot. Because `attach(null)` is not ordered behind completion of the asynchronous shutdown close, the coordinator can observe no player and skip the final session/progress close. This is especially relevant during process/service replacement or an app update, where the last server-acknowledged progress may otherwise lag what the listener actually heard. ## Expected behavior Service teardown must capture the final playback/session snapshot while the player required to produce it is still attached, and must order final local/server durability work explicitly before detaching that dependency. The fix must not rely on `onDestroy()` as the only durability mechanism for abrupt process death; existing periodic/local durability remains necessary. ## Investigation / implementation scope - Define the shutdown ordering contract between `PlaybackService` and `SessionSyncCoordinator`. - Add a direct `SessionSyncCoordinator.onShutdown()` regression test. - Prove the final snapshot is captured before player detachment. - Decide whether shutdown should synchronously capture an immutable snapshot first and hand that to asynchronous persistence/close work, or use another narrowly ordered mechanism. - Preserve cancellation semantics and avoid blocking Android service teardown on unbounded network work. - Keep profile/book ownership with the captured snapshot; do not re-resolve the active profile after teardown starts. - Keep logs redaction-safe. ## Out of scope - Do not redesign resume freshness; [GitHub PR #93](https://github.com/AlexanderWilhelmsenBerg/Audiobookshelf-Manager-Claude/pull/93) / `ResumeFreshnessCoordinator` remains the position-decision owner. - Do not fold this lifecycle repair into [GitHub PR #140](https://github.com/AlexanderWilhelmsenBerg/Audiobookshelf-Manager-Claude/pull/140) unless a later proof shows it is required for that narrow cold-resumption safety fix. - Do not add Android-Auto-specific behavior here; #45 tracks the separate cold Android Auto surfacing symptom. ## Acceptance criteria - A deterministic test fails with the current `onShutdown()` -> immediate `attach(null)` ordering and passes after the repair. - Final progress/session close uses the last valid player snapshot even when teardown continues immediately afterward. - Detaching/releasing the player cannot race the shutdown close into a no-op. - Normal Pause/seek/session-transition synchronization behavior remains unchanged. - Physical/process-replacement testing verifies progress does not regress across an `adb install -r` update where practical. ## Related - #43 — cold headset/background Play can resume the correct book at 0:00. - [GitHub PR #140](https://github.com/AlexanderWilhelmsenBerg/Audiobookshelf-Manager-Claude/pull/140) — narrow cold-resumption freshness validation; intentionally does not redesign teardown. - #45 — separate Android Auto cold/background surfacing symptom.
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
alexander/Audiobookshelf-Manager-Claude#46
No description provided.