From c99d9a6aed7546d54534ccb53f198dd5672b63a9 Mon Sep 17 00:00:00 2001 From: cloudhead Date: Sun, 8 Oct 2023 00:54:07 +0200 Subject: [PATCH] radicle: Make mock transport thread-safe The mock transport was not thread-safe when multiple threads used the same node ids. This is fixed by including the thread id as part of the storage key. --- radicle/src/storage/git/transport/remote/mock.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/radicle/src/storage/git/transport/remote/mock.rs b/radicle/src/storage/git/transport/remote/mock.rs index aef3b006..851af3e1 100644 --- a/radicle/src/storage/git/transport/remote/mock.rs +++ b/radicle/src/storage/git/transport/remote/mock.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use std::path::{Path, PathBuf}; use std::str::FromStr; use std::sync::{Mutex, Once}; +use std::thread::ThreadId; use std::{process, thread}; use once_cell::sync::Lazy; @@ -12,7 +13,8 @@ use crate::storage::git::transport::ChildStream; use crate::storage::RemoteId; /// Nodes registered with the mock transport. -static NODES: Lazy>> = Lazy::new(|| Mutex::new(HashMap::new())); +static NODES: Lazy>> = + Lazy::new(|| Mutex::new(HashMap::new())); /// The mock transport. #[derive(Default)] @@ -25,8 +27,9 @@ impl git2::transport::SmartSubtransport for MockTransport { service: git2::transport::Service, ) -> Result, git2::Error> { let url = Url::from_str(url).map_err(|e| git2::Error::from_str(e.to_string().as_str()))?; + let id = thread::current().id(); let nodes = NODES.lock().expect("lock cannot be poisoned"); - let storage = if let Some(storage) = nodes.get(&url.node) { + let storage = if let Some(storage) = nodes.get(&(id, url.node)) { match service { git2::transport::Service::ReceivePack | git2::transport::Service::ReceivePackLs => { return Err(git2::Error::from_str( @@ -89,9 +92,10 @@ pub fn register(node: &RemoteId, path: &Path) { }) .expect("transport registration is successful"); }); + let id = thread::current().id(); NODES .lock() .expect("the lock isn't poisoned") - .insert(*node, path.to_owned()); + .insert((id, *node), path.to_owned()); }