Skip to content

Commit 8049991

Browse files
authored
Fix race condition in local repo setup (#3693)
Signed-off-by: unlsycn <[email protected]>
1 parent 07ef2bc commit 8049991

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

modules/build/src/main/scala/scala/build/LocalRepo.scala

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,21 @@ object LocalRepo {
7777

7878
if !os.exists(repoDir) then
7979
withLock((repoDir / os.up).toNIO, version) {
80-
val tmpRepoDir = repoDir / os.up / s".$version.tmp"
81-
try os.remove.all(tmpRepoDir)
82-
catch {
83-
case t: Throwable =>
84-
logger.message(s"Error removing $tmpRepoDir: ${t.getMessage}")
85-
}
86-
using(archiveUrl.openStream()) { is =>
87-
using(WrappedZipInputStream.create(new BufferedInputStream(is))) { zis =>
88-
extractZip(zis, tmpRepoDir)
80+
// Post-lock validation: Recheck repository directory existence to handle
81+
// potential race conditions between initial check and lock acquisition
82+
if !os.exists(repoDir) then
83+
val tmpRepoDir = repoDir / os.up / s".$version.tmp"
84+
try os.remove.all(tmpRepoDir)
85+
catch {
86+
case t: Throwable =>
87+
logger.message(s"Error removing $tmpRepoDir: ${t.getMessage}")
8988
}
90-
}
91-
os.move(tmpRepoDir, repoDir)
89+
using(archiveUrl.openStream()) { is =>
90+
using(WrappedZipInputStream.create(new BufferedInputStream(is))) { zis =>
91+
extractZip(zis, tmpRepoDir)
92+
}
93+
}
94+
os.move(tmpRepoDir, repoDir)
9295
}
9396

9497
val repo = "ivy:" + repoDir.toNIO.toUri.toASCIIString + "/[defaultPattern]"

0 commit comments

Comments
 (0)