Fix persistence pause behavior

This commit is contained in:
Igor Katson 2024-08-23 17:47:28 +01:00
parent eb2b9e5a41
commit 6823490803
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
3 changed files with 10 additions and 3 deletions

View file

@ -1152,6 +1152,7 @@ impl Session {
));
let handle = Arc::new(ManagedTorrent {
locked: RwLock::new(ManagedTorrentLocked {
paused: opts.paused,
state: ManagedTorrentState::Initializing(initializing),
only_files,
}),
@ -1327,6 +1328,7 @@ impl Session {
}
pub async fn pause(&self, handle: &ManagedTorrentHandle) -> anyhow::Result<()> {
handle.locked.write().paused = true;
handle.pause()?;
self.try_update_persistence_metadata(handle).await;
Ok(())

View file

@ -8,7 +8,6 @@ use crate::{
storage::filesystem::FilesystemStorageFactory,
torrent_state::ManagedTorrentHandle,
type_aliases::BF,
ManagedTorrentState,
};
use anyhow::{bail, Context};
use async_trait::async_trait;
@ -146,7 +145,7 @@ impl JsonSessionPersistenceStore {
// we don't serialize this here, but to a file instead.
torrent_bytes: Default::default(),
only_files: torrent.only_files().clone(),
is_paused: torrent.with_state(|s| matches!(s, ManagedTorrentState::Paused(_))),
is_paused: torrent.is_paused(),
output_folder: torrent.shared().options.output_folder.clone(),
};

View file

@ -85,6 +85,11 @@ impl ManagedTorrentState {
}
pub(crate) struct ManagedTorrentLocked {
// The torrent might not be in "paused" state technically,
// but the intention might be for it to stay paused.
//
// This should change only on "unpause".
pub(crate) paused: bool,
pub(crate) state: ManagedTorrentState,
pub(crate) only_files: Option<Vec<usize>>,
}
@ -218,6 +223,7 @@ impl ManagedTorrent {
.upgrade()
.context("session is dead, cannot start torrent")?;
let mut g = self.locked.write();
g.paused = start_paused;
let cancellation_token = session.cancellation_token().child_token();
let spawn_fatal_errors_receiver =
@ -380,7 +386,7 @@ impl ManagedTorrent {
}
pub fn is_paused(&self) -> bool {
self.with_state(|s| matches!(s, ManagedTorrentState::Paused(..)))
self.locked.read().paused
}
/// Pause the torrent if it's live.