Skip to content

Commit 8f8a5f0

Browse files
committed
fix potential race condition when parallel requests download the same archive index
1 parent 318ab7b commit 8f8a5f0

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/storage/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,16 @@ impl Storage {
301301
.parent()
302302
.ok_or_else(|| anyhow!("index path without parent"))?,
303303
)?;
304-
let mut file = fs::File::create(&local_index_path)?;
305-
file.write_all(&index_content)?;
304+
305+
// when we don't have a locally cached index and many parallel request
306+
// we might download the same archive index multiple times here.
307+
// So we're storing the content into a temporary file before renaming it
308+
// into the final location.
309+
let mut tmpfile =
310+
tempfile::NamedTempFile::new_in(&self.config.local_archive_cache_path)?;
311+
tmpfile.write_all(&index_content)?;
312+
let temp_path = tmpfile.into_temp_path();
313+
fs::rename(temp_path, &local_index_path)?;
306314
}
307315

308316
Ok(local_index_path)

0 commit comments

Comments
 (0)