Skip to content

Commit

Permalink
Do not wait for the write lock (#7130)
Browse files Browse the repository at this point in the history
in `BuildArtifactCacheDirectory` as it may cause a deadlock if the
wait queue looks like "W", "R".. when the read lock is already held.

The first read lock stamp is released only after all reads are processed
and they won't until the write is processed but it can't proceed.

Bug: 373957467
Test: n/a
Change-Id: Ice28bae0288296fa8ed17c31187b578affb8d46c

AOSP: cff9f98e002cc7f7d7d379090f9dd4655221a278

Co-authored-by: Googler <[email protected]>
Co-authored-by: Błażej Kardyś <[email protected]>
  • Loading branch information
3 people authored Jan 9, 2025
1 parent a02a96e commit 2417d58
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,11 @@ private FileTime readAccessTime(Path entry) throws IOException {
@Override
public void clean(long maxTargetSizeBytes, Duration minKeepDuration) throws BuildException {
// Ensure that no artifacts are added or read from the cache while we're cleaning:
long stamp = lock.writeLock();
long stamp = lock.tryWriteLock();
if (stamp == 0) {
logger.warning("Failed to clean the build cache at " + cacheDir + " Failed to obtain the write lock");
return; // Just exit. WE will clean the cache next time.
}
try {
needClean = false;
clean(maxTargetSizeBytes, Instant.now().minus(minKeepDuration));
Expand Down Expand Up @@ -467,7 +471,11 @@ void clean(long maxTargetSize, Instant minAgeToDelete) throws IOException {

public void purge() throws BuildException {
// Ensure that no artifacts are added or read from the cache while we're cleaning:
long stamp = lock.writeLock();
long stamp = lock.tryWriteLock();
if (stamp == 0) {
// TODO: b/373957467 - Report this error to the user properly.
throw new BuildException("Failed to purge the build artifact cache. Cannot obtain the write lock.");
}
try {
MoreFiles.deleteDirectoryContents(cacheDir);
} catch (IOException e) {
Expand Down

0 comments on commit 2417d58

Please sign in to comment.