diff --git a/crates/librqbit/src/session_persistence/json.rs b/crates/librqbit/src/session_persistence/json.rs index 7ed44b5..cda4e69 100644 --- a/crates/librqbit/src/session_persistence/json.rs +++ b/crates/librqbit/src/session_persistence/json.rs @@ -85,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) @@ -96,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)