Skip to content

Terminal interface to find, download, and stream movies, TV shows, and live TV using local media players.

fix(playback): enforce active player session ownership and single-session invariant

Samir Hossainsb75b75b422a2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 719a127..9e60149 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,7 +4,7 @@
### Added
- **Comprehensive QA & Regression Test Architecture**:
- - Introduced a 62-test automated suite covering critical algorithmic boundaries, end-to-end user journeys, duplicate action & rapid double-click debounce protection, error handling, addon manifest validation, mouse interactions, modal dismissals, TUI rendering across terminal size matrices, state reconciliation, crypto HMAC signing, download chunk arithmetic, and URL/stem security.
+ - Introduced a 63-test automated suite covering critical algorithmic boundaries, end-to-end user journeys, active player session lifecycle & duplicate launch protection, error handling, addon manifest validation, mouse interactions, modal dismissals, TUI rendering across terminal size matrices, state reconciliation, crypto HMAC signing, download chunk arithmetic, and URL/stem security.
- Added structured integration tests in `tests/` (`error_handling.rs`, `tui_acceptance.rs`, `history_reconciliation.rs`, `cache_lifecycle.rs`, `player_integration.rs`, `m3u_integration.rs`, `addons_manifest.rs`, `download_integration.rs`, `url_security.rs`) and test fixtures (`tests/fixtures/`).
- Added [`docs/testing.md`](docs/testing.md) detailing test architecture, command references, and manual QA procedures.
- **Playback Tracking & Watch History Progress**:
diff --git a/src/tui/action.rs b/src/tui/action.rs
index 67e5ddb..f253152 100644
--- a/src/tui/action.rs
+++ b/src/tui/action.rs
@@ -140,4 +140,5 @@ pub enum Action {
},
ReconcileHistory,
PlayerCrashed(Option<i32>, String),
+ PlayerExited,
}
diff --git a/src/tui/app/mouse.rs b/src/tui/app/mouse.rs
index 5f7d6a8..e56e07a 100644
--- a/src/tui/app/mouse.rs
+++ b/src/tui/app/mouse.rs
@@ -1,6 +1,7 @@
use super::App;
use crate::tui::{
action::Action,
+ overlay::NotificationKind,
state::{BrowsePreset, DetailsPane, InputMode, Screen},
};
use ratatui::layout::{Constraint, Direction, Layout, Rect};
@@ -708,11 +709,18 @@ impl App {
let prev_selected = self.state.resource_list_state.selected();
self.state.resource_list_state.select(Some(target_idx));
- if prev_selected == Some(target_idx)
- && !self.state.is_resolving_playback
- && self.state.last_playback_launch.elapsed().as_millis() >= 500
- {
- self.action_sender.send(Action::PlayStream(false)).ok();
+ if prev_selected == Some(target_idx) {
+ if self.state.is_playing {
+ self.state.notify(
+ NotificationKind::Warning,
+ "Playback already active",
+ "Stop the current player before starting another.",
+ );
+ } else if !self.state.is_resolving_playback
+ && self.state.last_playback_launch.elapsed().as_millis() >= 500
+ {
+ self.action_sender.send(Action::PlayStream(false)).ok();
+ }
}
}
return None;
diff --git a/src/tui/app/playback.rs b/src/tui/app/playback.rs
index 97e512a..47186f9 100644
--- a/src/tui/app/playback.rs
+++ b/src/tui/app/playback.rs
@@ -128,6 +128,9 @@ impl App {
None
};
+ self.state.is_playing = true;
+ self.state.is_resolving_playback = false;
+
let tracker_opts = history_item.as_ref().map(|item| {
(
item.provider.clone(),
@@ -264,6 +267,7 @@ impl App {
if let Some(path) = temporary_subtitle {
let _ = std::fs::remove_file(path);
}
+ sender.send(Action::PlayerExited).ok();
});
}
Err(error) => {
@@ -281,6 +285,7 @@ impl App {
format!("Failed to spawn player executable: {error}"),
))
.ok();
+ sender.send(Action::PlayerExited).ok();
}
}
});
@@ -291,6 +296,14 @@ impl App {
pub(super) async fn handle_playback(&mut self, action: Action) -> Option<()> {
match action {
Action::PlayStream(open_with) => {
+ if self.state.is_playing {
+ self.state.notify(
+ NotificationKind::Warning,
+ "Playback already active",
+ "Stop the current player before starting another.",
+ );
+ return None;
+ }
if self.state.is_resolving_playback
|| self.state.last_playback_launch.elapsed().as_millis() < 500
{
@@ -511,6 +524,14 @@ impl App {
}
}
Action::LaunchMpv(link, subtitle_url) => {
+ if self.state.is_playing {
+ self.state.notify(
+ NotificationKind::Warning,
+ "Playback already active",
+ "Stop the current player before starting another.",
+ );
+ return None;
+ }
if self.state.last_playback_launch.elapsed().as_millis() < 500 {
return None;
}
@@ -622,7 +643,13 @@ impl App {
Action::ReconcileHistory => {
self.state.history.reconcile_pending_playback_states();
}
+ Action::PlayerExited => {
+ self.state.is_playing = false;
+ self.state.is_resolving_playback = false;
+ }
Action::PlayerCrashed(code, error_msg) => {
+ self.state.is_playing = false;
+ self.state.is_resolving_playback = false;
let code_str = code
.map(|c| c.to_string())
.unwrap_or_else(|| "unknown".into());
diff --git a/src/tui/app/run.rs b/src/tui/app/run.rs
index e531820..74fde78 100644
--- a/src/tui/app/run.rs
+++ b/src/tui/app/run.rs
@@ -226,7 +226,8 @@ impl App {
| Action::MarkWatched(..)
| Action::UpdateProgress { .. }
| Action::ReconcileHistory
- | Action::PlayerCrashed(..) => {
+ | Action::PlayerCrashed(..)
+ | Action::PlayerExited => {
self.handle_playback(action).await;
}
diff --git a/src/tui/state.rs b/src/tui/state.rs
index d3c9824..c7c91a5 100644
--- a/src/tui/state.rs
+++ b/src/tui/state.rs
@@ -128,6 +128,7 @@ pub struct AppState {
pub default_player: Option<String>,
pub is_loading: bool,
pub is_resolving_playback: bool,
+ pub is_playing: bool,
pub last_playback_launch: std::time::Instant,
pub status_message: String,
pub status_timer: usize,
@@ -267,6 +268,7 @@ impl Default for AppState {
dirty: true,
is_loading: false,
is_resolving_playback: false,
+ is_playing: false,
last_playback_launch: std::time::Instant::now()
.checked_sub(std::time::Duration::from_secs(5))
.unwrap_or_else(std::time::Instant::now),
diff --git a/tests/error_handling.rs b/tests/error_handling.rs
index b36b369..f16911f 100644
--- a/tests/error_handling.rs
+++ b/tests/error_handling.rs
@@ -128,3 +128,20 @@ async fn test_rapid_playback_invocations_debounced_and_single_flight() {
assert_eq!(res, None);
assert!(!app.state().is_resolving_playback);
}
+
+#[tokio::test]
+async fn test_active_player_session_blocks_duplicate_playback_and_recovers_on_exit() {
+ let mut app = App::new();
+ app.state_mut().is_playing = true;
+
+ let res = app.handle_action(Action::PlayStream(false)).await;
+ assert_eq!(res, None);
+ assert!(!app.state().notifications.is_empty());
+ let notif = app.state().notifications.back().unwrap();
+ assert_eq!(notif.kind, NotificationKind::Warning);
+ assert_eq!(notif.title, "Playback already active");
+
+ app.handle_action(Action::PlayerExited).await;
+ assert!(!app.state().is_playing);
+ assert!(!app.state().is_resolving_playback);
+}