diff --git a/crates/brioche-core/src/project.rs b/crates/brioche-core/src/project.rs index 9b7feed..0aa799b 100644 --- a/crates/brioche-core/src/project.rs +++ b/crates/brioche-core/src/project.rs @@ -985,6 +985,17 @@ async fn fetch_project_from_registry( brioche: &Brioche, project_hash: ProjectHash, ) -> anyhow::Result { + // Use a mutex to ensure we don't try to fetch the same project more + // than once at a time + static FETCH_PROJECTS_MUTEX: tokio::sync::Mutex< + BTreeMap>>, + > = tokio::sync::Mutex::const_new(BTreeMap::new()); + let project_mutex = { + let mut fetch_projects = FETCH_PROJECTS_MUTEX.lock().await; + fetch_projects.entry(project_hash).or_default().clone() + }; + let _guard = project_mutex.lock().await; + let local_path = brioche.home.join("projects").join(project_hash.to_string()); if tokio::fs::try_exists(&local_path).await? {