Lock the toolchains directory during toolchain operations (#4733)

This commit is contained in:
Zanie Blue 2024-07-02 14:31:30 -04:00 committed by GitHub
parent a380e8e4df
commit c8987269ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 14 additions and 1 deletions

View file

@ -19,7 +19,7 @@ use crate::platform::{Arch, Libc, Os};
use crate::python_version::PythonVersion; use crate::python_version::PythonVersion;
use crate::toolchain::{self, ToolchainKey}; use crate::toolchain::{self, ToolchainKey};
use crate::ToolchainRequest; use crate::ToolchainRequest;
use uv_fs::Simplified; use uv_fs::{LockedFile, Simplified};
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum Error { pub enum Error {
@ -65,6 +65,14 @@ impl InstalledToolchains {
Self { root: root.into() } Self { root: root.into() }
} }
/// Lock the toolchains directory.
pub fn acquire_lock(&self) -> Result<LockedFile, Error> {
Ok(LockedFile::acquire(
self.root.join(".lock"),
self.root.user_display(),
)?)
}
/// Prefer, in order: /// Prefer, in order:
/// 1. The specific toolchain directory specified by the user, i.e., `UV_TOOLCHAIN_DIR` /// 1. The specific toolchain directory specified by the user, i.e., `UV_TOOLCHAIN_DIR`
/// 2. A directory in the system-appropriate user-level data directory, e.g., `~/.local/uv/toolchains` /// 2. A directory in the system-appropriate user-level data directory, e.g., `~/.local/uv/toolchains`
@ -224,6 +232,7 @@ impl InstalledToolchain {
Ok(Self { path, key }) Ok(Self { path, key })
} }
/// The path to this toolchain's Python executable.
pub fn executable(&self) -> PathBuf { pub fn executable(&self) -> PathBuf {
if cfg!(windows) { if cfg!(windows) {
self.path.join("install").join("python.exe") self.path.join("install").join("python.exe")
@ -234,6 +243,7 @@ impl InstalledToolchain {
} }
} }
/// The [`PythonVersion`] of the toolchain.
pub fn version(&self) -> PythonVersion { pub fn version(&self) -> PythonVersion {
self.key.version() self.key.version()
} }

View file

@ -122,6 +122,7 @@ impl Toolchain {
) -> Result<Self, Error> { ) -> Result<Self, Error> {
let toolchains = InstalledToolchains::from_settings()?.init()?; let toolchains = InstalledToolchains::from_settings()?.init()?;
let toolchain_dir = toolchains.root(); let toolchain_dir = toolchains.root();
let _lock = toolchains.acquire_lock()?;
let download = PythonDownload::from_request(&request)?; let download = PythonDownload::from_request(&request)?;
let client = client_builder.build(); let client = client_builder.build();

View file

@ -31,6 +31,7 @@ pub(crate) async fn install(
let toolchains = InstalledToolchains::from_settings()?.init()?; let toolchains = InstalledToolchains::from_settings()?.init()?;
let toolchain_dir = toolchains.root(); let toolchain_dir = toolchains.root();
let _lock = toolchains.acquire_lock()?;
let requests: Vec<_> = if targets.is_empty() { let requests: Vec<_> = if targets.is_empty() {
if let Some(requests) = requests_from_version_file().await? { if let Some(requests) = requests_from_version_file().await? {

View file

@ -23,6 +23,7 @@ pub(crate) async fn uninstall(
} }
let toolchains = InstalledToolchains::from_settings()?.init()?; let toolchains = InstalledToolchains::from_settings()?.init()?;
let _lock = toolchains.acquire_lock()?;
let requests = targets let requests = targets
.iter() .iter()