Skip to content

Commit

Permalink
Try to make PathRef robust against concurrent filesystem modificati…
Browse files Browse the repository at this point in the history
…ons (#2832)

Attempts to fix #2808

No real tests; the failure is timing-dependent and hard to reproduce.
But hopefully just wrapping everything up in a big try-catch will fix it

Pull request: #2832
  • Loading branch information
lihaoyi authored Oct 9, 2023
1 parent 09892e8 commit 1cd494b
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions main/api/src/mill/api/PathRef.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,28 +125,38 @@ object PathRef {
val sub = path.subRelativeTo(basePath)
digest.update(sub.toString().getBytes())
if (!attrs.isDir) {
if (isPosix) {
updateWithInt(os.perms(path, followLinks = false).value)
}
if (quick) {
val value = (attrs.mtime, attrs.size).hashCode()
updateWithInt(value)
} else if (jnio.Files.isReadable(path.toNIO)) {
val is =
try Some(os.read.inputStream(path))
catch {
case _: jnio.FileSystemException =>
// This is known to happen, when we try to digest a socket file.
// We ignore the content of this file for now, as we would do,
// when the file isn't readable.
// See https://github.com/com-lihaoyi/mill/issues/1875
None
}
is.foreach {
Using.resource(_) { is =>
StreamSupport.stream(is, digestOut)
try {
if (isPosix) {
updateWithInt(os.perms(path, followLinks = false).value)
}
if (quick) {
val value = (attrs.mtime, attrs.size).hashCode()
updateWithInt(value)
} else if (jnio.Files.isReadable(path.toNIO)) {
val is =
try Some(os.read.inputStream(path))
catch {
case _: jnio.FileSystemException =>
// This is known to happen, when we try to digest a socket file.
// We ignore the content of this file for now, as we would do,
// when the file isn't readable.
// See https://github.com/com-lihaoyi/mill/issues/1875
None
}
is.foreach {
Using.resource(_) { is =>
StreamSupport.stream(is, digestOut)
}
}
}
} catch {
case e: java.nio.file.NoSuchFileException =>
// If file was deleted after we listed the folder but before we operate on it,
// `os.perms` or `os.read.inputStream` will crash. In that case, just do nothing,
// so next time we calculate the `PathRef` we'll get a different hash signature
// (either with the file missing, or with the file present) and invalidate any
// caches

}
}
}
Expand Down

0 comments on commit 1cd494b

Please sign in to comment.