Skip to content

Commit

Permalink
Apply advisory locks when building source distributions
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed May 11, 2024
1 parent 6fd6868 commit eda9cb3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/uv-cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ impl CacheShard {
pub fn shard(&self, dir: impl AsRef<Path>) -> Self {
Self(self.0.join(dir.as_ref()))
}

/// Acquire a lock on the shard.
pub fn lock(&self) -> io::Result<uv_fs::LockedFile> {
fs_err::create_dir_all(&self.0)?;
uv_fs::LockedFile::acquire(self.0.join(".lock"), self.0.display())
}
}

impl AsRef<Path> for CacheShard {
Expand Down
16 changes: 16 additions & 0 deletions crates/uv-distribution/src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ impl<'a, T: BuildContext> SourceDistributionBuilder<'a, T> {
hashes: HashPolicy<'_>,
client: &ManagedClient<'_>,
) -> Result<BuiltWheelMetadata, Error> {
let _lock = cache_shard.lock().map_err(Error::CacheWrite)?;

// Fetch the revision for the source distribution.
let revision = self
.url_revision(source, filename, url, cache_shard, hashes, client)
Expand Down Expand Up @@ -465,6 +467,8 @@ impl<'a, T: BuildContext> SourceDistributionBuilder<'a, T> {
hashes: HashPolicy<'_>,
client: &ManagedClient<'_>,
) -> Result<ArchiveMetadata, Error> {
let _lock = cache_shard.lock().map_err(Error::CacheWrite)?;

// Fetch the revision for the source distribution.
let revision = self
.url_revision(source, filename, url, cache_shard, hashes, client)
Expand Down Expand Up @@ -625,6 +629,8 @@ impl<'a, T: BuildContext> SourceDistributionBuilder<'a, T> {
tags: &Tags,
hashes: HashPolicy<'_>,
) -> Result<BuiltWheelMetadata, Error> {
let _lock = cache_shard.lock().map_err(Error::CacheWrite)?;

// Fetch the revision for the source distribution.
let revision = self
.archive_revision(source, resource, cache_shard, hashes)
Expand Down Expand Up @@ -691,6 +697,8 @@ impl<'a, T: BuildContext> SourceDistributionBuilder<'a, T> {
cache_shard: &CacheShard,
hashes: HashPolicy<'_>,
) -> Result<ArchiveMetadata, Error> {
let _lock = cache_shard.lock().map_err(Error::CacheWrite)?;

// Fetch the revision for the source distribution.
let revision = self
.archive_revision(source, resource, cache_shard, hashes)
Expand Down Expand Up @@ -838,6 +846,8 @@ impl<'a, T: BuildContext> SourceDistributionBuilder<'a, T> {
WheelCache::Path(resource.url).root(),
);

let _lock = cache_shard.lock().map_err(Error::CacheWrite)?;

// Fetch the revision for the source distribution.
let revision = self
.source_tree_revision(source, resource, &cache_shard)
Expand Down Expand Up @@ -902,6 +912,8 @@ impl<'a, T: BuildContext> SourceDistributionBuilder<'a, T> {
WheelCache::Path(resource.url).root(),
);

let _lock = cache_shard.lock().map_err(Error::CacheWrite)?;

// Fetch the revision for the source distribution.
let revision = self
.source_tree_revision(source, resource, &cache_shard)
Expand Down Expand Up @@ -1039,6 +1051,8 @@ impl<'a, T: BuildContext> SourceDistributionBuilder<'a, T> {
WheelCache::Git(&url, &git_sha.to_short_string()).root(),
);

let _lock = cache_shard.lock().map_err(Error::CacheWrite)?;

// If the cache contains a compatible wheel, return it.
if let Some(built_wheel) = BuiltWheelMetadata::find_in_cache(tags, &cache_shard) {
return Ok(built_wheel);
Expand Down Expand Up @@ -1111,6 +1125,8 @@ impl<'a, T: BuildContext> SourceDistributionBuilder<'a, T> {
WheelCache::Git(&url, &git_sha.to_short_string()).root(),
);

let _lock = cache_shard.lock().map_err(Error::CacheWrite)?;

// If the cache contains compatible metadata, return it.
let metadata_entry = cache_shard.entry(METADATA);
if self
Expand Down

0 comments on commit eda9cb3

Please sign in to comment.