Fix concurrent write bug for session.json

This commit is contained in:
Igor Katson 2024-08-23 17:50:38 +01:00
parent 6823490803
commit 673f59009a
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5

View file

@ -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)