Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix persistence bugs #207

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/librqbit/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,7 @@ impl Session {
));
let handle = Arc::new(ManagedTorrent {
locked: RwLock::new(ManagedTorrentLocked {
paused: opts.paused,
state: ManagedTorrentState::Initializing(initializing),
only_files,
}),
Expand Down Expand Up @@ -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(())
Expand Down
8 changes: 4 additions & 4 deletions crates/librqbit/src/session_persistence/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,6 +85,8 @@ impl JsonSessionPersistenceStore {
}

async fn flush(&self) -> anyhow::Result<()> {
// we don't need the write lock technically, but we need to stop concurrent modifications
let db_content = self.db_content.write().await;
let tmp_filename = format!("{}.tmp", self.db_filename.to_str().unwrap());
let mut tmp = tokio::fs::OpenOptions::new()
.create(true)
Expand All @@ -97,8 +98,7 @@ impl JsonSessionPersistenceStore {
trace!(?tmp_filename, "opened temp file");

let mut buf = Vec::new();
serde_json::to_writer(&mut buf, &*self.db_content.read().await)
.context("error serializing")?;
serde_json::to_writer(&mut buf, &*db_content).context("error serializing")?;

trace!(?tmp_filename, "serialized DB as JSON");
tmp.write_all(&buf)
Expand Down Expand Up @@ -146,7 +146,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(),
};

Expand Down
8 changes: 7 additions & 1 deletion crates/librqbit/src/torrent_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
}
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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.
Expand Down