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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ pub trait Backend: Debug + Send + Sync {

fn list_remote_versions(&self) -> eyre::Result<Vec<String>> {
self.get_remote_version_cache()
// TODO: fix deadlock
.get_or_try_init(|| {
trace!("Listing remote versions for {}", self.ba().to_string());
match versions_host::list_versions(self.ba()) {
Expand Down
1 change: 1 addition & 0 deletions src/backend/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl Backend for NPMBackend {
timeout::run_with_timeout(
|| {
self.latest_version_cache
// TODO: fix deadlock
.get_or_try_init(|| {
let raw =
cmd!(NPM_PROGRAM, "view", self.tool_name(), "dist-tags", "--json")
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