Skip to content

fix: fix deadlocks caused by uv_venv_auto #4900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 37 additions & 33 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,37 +198,40 @@ pub trait Backend: Debug + Send + Sync {
}

fn list_remote_versions(&self) -> eyre::Result<Vec<String>> {
self.get_remote_version_cache()
.get_or_try_init(|| {
trace!("Listing remote versions for {}", self.ba().to_string());
match versions_host::list_versions(self.ba()) {
Ok(Some(versions)) => return Ok(versions),
Ok(None) => {}
Err(e) => {
debug!("Error getting versions from versions host: {:#}", e);
}
};
trace!(
"Calling backend to list remote versions for {}",
self.ba().to_string()
);
let versions = self
._list_remote_versions()?
.into_iter()
.filter(|v| match v.parse::<ToolVersionType>() {
Ok(ToolVersionType::Version(_)) => true,
_ => {
warn!("Invalid version: {}@{v}", self.id());
false
}
})
.collect_vec();
if versions.is_empty() {
warn!("No versions found for {}", self.id());
let fetch = || {
trace!("Listing remote versions for {}", self.ba().to_string());
match versions_host::list_versions(self.ba()) {
Ok(Some(versions)) => return Ok(versions),
Ok(None) => {}
Err(e) => {
debug!("Error getting versions from versions host: {:#}", e);
}
Ok(versions)
})
.cloned()
};
trace!(
"Calling backend to list remote versions for {}",
self.ba().to_string()
);
let versions = self
._list_remote_versions()?
.into_iter()
.filter(|v| match v.parse::<ToolVersionType>() {
Ok(ToolVersionType::Version(_)) => true,
_ => {
warn!("Invalid version: {}@{v}", self.id());
false
}
})
.collect_vec();
if versions.is_empty() {
warn!("No versions found for {}", self.id());
}
Ok(versions)
};
if let Ok(cache) = self.get_remote_version_cache().try_lock() {
cache.get_or_try_init(fetch).cloned()
} else {
fetch()
}
}
fn _list_remote_versions(&self) -> eyre::Result<Vec<String>>;
fn latest_stable_version(&self) -> eyre::Result<Option<String>> {
Expand Down Expand Up @@ -626,8 +629,9 @@ pub trait Backend: Debug + Send + Sync {
Ok(versions)
}

fn get_remote_version_cache(&self) -> Arc<VersionCacheManager> {
static REMOTE_VERSION_CACHE: Lazy<Mutex<HashMap<String, Arc<VersionCacheManager>>>> =
fn get_remote_version_cache(&self) -> Arc<Mutex<VersionCacheManager>> {
// use a mutex to prevent deadlocks that occurs due to reentrant cache access
static REMOTE_VERSION_CACHE: Lazy<Mutex<HashMap<String, Arc<Mutex<VersionCacheManager>>>>> =
Lazy::new(|| Mutex::new(HashMap::new()));

REMOTE_VERSION_CACHE
Expand All @@ -645,7 +649,7 @@ pub trait Backend: Debug + Send + Sync {
.with_fresh_file(plugin_path.join("bin/list-all"))
}

Arc::new(cm.build())
Mutex::new(cm.build()).into()
})
.clone()
}
Expand Down
43 changes: 23 additions & 20 deletions src/backend/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ use crate::timeout;
use crate::toolset::ToolVersion;
use serde_json::Value;
use std::fmt::Debug;
use std::sync::Mutex;

#[derive(Debug)]
pub struct NPMBackend {
ba: BackendArg,
latest_version_cache: CacheManager<Option<String>>,
// use a mutex to prevent deadlocks that occurs due to reentrant cache access
latest_version_cache: Mutex<CacheManager<Option<String>>>,
}

const NPM_PROGRAM: &str = if cfg!(windows) { "npm.cmd" } else { "npm" };
Expand Down Expand Up @@ -45,22 +47,23 @@ impl Backend for NPMBackend {
}

fn latest_stable_version(&self) -> eyre::Result<Option<String>> {
let fetch = || {
let raw = cmd!(NPM_PROGRAM, "view", self.tool_name(), "dist-tags", "--json")
.full_env(self.dependency_env()?)
.read()?;
let dist_tags: Value = serde_json::from_str(&raw)?;
match dist_tags["latest"] {
Value::String(ref s) => Ok(Some(s.clone())),
_ => self.latest_version(Some("latest".into())),
}
};
timeout::run_with_timeout(
|| {
self.latest_version_cache
.get_or_try_init(|| {
let raw =
cmd!(NPM_PROGRAM, "view", self.tool_name(), "dist-tags", "--json")
.full_env(self.dependency_env()?)
.read()?;
let dist_tags: Value = serde_json::from_str(&raw)?;
let latest = match dist_tags["latest"] {
Value::String(ref s) => Some(s.clone()),
_ => self.latest_version(Some("latest".into())).unwrap(),
};
Ok(latest)
})
.cloned()
if let Ok(cache) = self.latest_version_cache.try_lock() {
cache.get_or_try_init(fetch).cloned()
} else {
fetch()
}
},
SETTINGS.fetch_remote_versions_timeout(),
)
Expand Down Expand Up @@ -112,11 +115,11 @@ impl Backend for NPMBackend {
impl NPMBackend {
pub fn from_arg(ba: BackendArg) -> Self {
Self {
latest_version_cache: CacheManagerBuilder::new(
ba.cache_path.join("latest_version.msgpack.z"),
)
.with_fresh_duration(SETTINGS.fetch_remote_versions_cache())
.build(),
latest_version_cache: Mutex::new(
CacheManagerBuilder::new(ba.cache_path.join("latest_version.msgpack.z"))
.with_fresh_duration(SETTINGS.fetch_remote_versions_cache())
.build(),
),
ba,
}
}
Expand Down
16 changes: 10 additions & 6 deletions src/plugins/core/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use eyre::{Result, bail, ensure};
use serde_derive::Deserialize;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, OnceLock};
use std::sync::{Arc, Mutex, OnceLock};
use tempfile::tempdir_in;
use url::Url;
use xx::regex;
Expand Down Expand Up @@ -448,16 +448,20 @@ impl Backend for NodePlugin {
Ok(vec![tv.install_path()])
}

fn get_remote_version_cache(&self) -> Arc<VersionCacheManager> {
static CACHE: OnceLock<Arc<VersionCacheManager>> = OnceLock::new();
fn get_remote_version_cache(&self) -> Arc<Mutex<VersionCacheManager>> {
static CACHE: OnceLock<Arc<Mutex<VersionCacheManager>>> = OnceLock::new();
CACHE
.get_or_init(|| {
CacheManagerBuilder::new(self.ba().cache_path.join("remote_versions.msgpack.z"))
Mutex::new(
CacheManagerBuilder::new(
self.ba().cache_path.join("remote_versions.msgpack.z"),
)
.with_fresh_duration(SETTINGS.fetch_remote_versions_cache())
.with_cache_key(SETTINGS.node.mirror_url.clone().unwrap_or_default())
.with_cache_key(SETTINGS.node.flavor.clone().unwrap_or_default())
.build()
.into()
.build(),
)
.into()
})
.clone()
}
Expand Down
16 changes: 10 additions & 6 deletions src/plugins/core/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use itertools::Itertools;
use std::collections::BTreeMap;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::LazyLock as Lazy;
use std::sync::{Arc, OnceLock};
use std::sync::{LazyLock as Lazy, Mutex};
use versions::Versioning;
use xx::regex;

Expand Down Expand Up @@ -474,15 +474,19 @@ impl Backend for PythonPlugin {
Ok(hm)
}

fn get_remote_version_cache(&self) -> Arc<VersionCacheManager> {
static CACHE: OnceLock<Arc<VersionCacheManager>> = OnceLock::new();
fn get_remote_version_cache(&self) -> Arc<Mutex<VersionCacheManager>> {
static CACHE: OnceLock<Arc<Mutex<VersionCacheManager>>> = OnceLock::new();
CACHE
.get_or_init(|| {
CacheManagerBuilder::new(self.ba().cache_path.join("remote_versions.msgpack.z"))
Mutex::new(
CacheManagerBuilder::new(
self.ba().cache_path.join("remote_versions.msgpack.z"),
)
.with_fresh_duration(SETTINGS.fetch_remote_versions_cache())
.with_cache_key((SETTINGS.python.compile == Some(false)).to_string())
.build()
.into()
.build(),
)
.into()
})
.clone()
}
Expand Down
12 changes: 6 additions & 6 deletions src/toolset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::hooks::Hooks;
use crate::install_context::InstallContext;
use crate::path_env::PathEnv;
use crate::ui::multi_progress_report::MultiProgressReport;
use crate::uv::UV_VENV;
use crate::uv::get_uv_venv;
use crate::{backend, config, env, hooks};
pub use builder::ToolsetBuilder;
use console::truncate_str;
Expand Down Expand Up @@ -527,9 +527,9 @@ impl Toolset {
env.insert(PATH_KEY.to_string(), add_paths);
}
env.extend(config.env()?.clone());
if let Ok(Some(venv)) = UV_VENV.try_lock().as_deref() {
for (k, v) in &venv.env {
env.insert(k.clone(), v.clone());
if let Some(venv) = get_uv_venv() {
for (k, v) in venv.env {
env.insert(k, v);
}
}
time!("env end");
Expand Down Expand Up @@ -581,8 +581,8 @@ impl Toolset {
for p in config.path_dirs()?.clone() {
paths.insert(p);
}
if let Ok(Some(venv)) = UV_VENV.try_lock().as_deref() {
paths.insert(venv.venv_path.clone());
if let Some(venv) = get_uv_venv() {
paths.insert(venv.venv_path);
}
if let Some(path) = self.env(config)?.get(&*PATH_KEY) {
paths.insert(PathBuf::from(path));
Expand Down
19 changes: 12 additions & 7 deletions src/uv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,33 @@ use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{LazyLock as Lazy, Mutex};

#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct Venv {
pub venv_path: PathBuf,
pub env: HashMap<String, String>,
}

// use a mutex to prevent deadlocks that may occur due to recursive initialization
// use a mutex to prevent deadlocks that occurs due to reentrantly initialization
// when resolving the venv path or env vars
pub static UV_VENV: Lazy<Mutex<Option<Venv>>> = Lazy::new(|| {
static UV_VENV: Mutex<Lazy<Option<Venv>>> = Mutex::new(Lazy::new(|| {
if !SETTINGS.python.uv_venv_auto {
return Mutex::new(None);
return None;
}
if let (Some(venv_path), Some(uv_path)) = (venv_path(), uv_path()) {
match get_or_create_venv(venv_path, uv_path) {
Ok(venv) => return Mutex::new(Some(venv)),
Ok(venv) => return Some(venv),
Err(e) => {
warn!("uv venv failed: {e}");
}
}
}
Mutex::new(None)
});
None
}));

pub fn get_uv_venv() -> Option<Venv> {
let uv_venv = UV_VENV.try_lock().ok()?;
uv_venv.as_ref().cloned()
}

fn get_or_create_venv(venv_path: PathBuf, uv_path: PathBuf) -> Result<Venv> {
SETTINGS.ensure_experimental("uv venv auto")?;
Expand Down
Loading