From fb9a884b48b5265dfa82b8cf0d5dbe475c7454df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Jourdan-Weil?= Date: Thu, 2 Jan 2025 18:30:41 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(temporaryfilecreator):=20do?= =?UTF-8?q?=20not=20try=20to=20delete=20not=20created=20temporary=20folder?= =?UTF-8?q?=20(fix=20#13013)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With a read-only filesystem, an error was raised during shutdown when trying to delete the temporary folder because delete would first create the folder if it was not yet created. fix #13013 --- .../src/main/scala/play/api/libs/Files.scala | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/core/play/src/main/scala/play/api/libs/Files.scala b/core/play/src/main/scala/play/api/libs/Files.scala index 9093e5bc672..281d126b02f 100644 --- a/core/play/src/main/scala/play/api/libs/Files.scala +++ b/core/play/src/main/scala/play/api/libs/Files.scala @@ -256,11 +256,13 @@ object Files { // Keeping references ensures that the FinalizablePhantomReference itself is not garbage-collected. private val references = Sets.newConcurrentHashSet[Reference[TemporaryFile]]() - private val TempDirectoryPrefix = "playtemp" - private lazy val playTempFolder: Path = { + private val TempDirectoryPrefix = "playtemp" + private var _playTempFolder: Option[Path] = None + private def playTempFolder: Path = _playTempFolder.getOrElse { val dir = Paths.get(conf.get[String]("play.temporaryFile.dir")) JFiles.createDirectories(dir) // make sure dir exists, otherwise createTempDirectory fails val tmpFolder = JFiles.createTempDirectory(dir, TempDirectoryPrefix) + _playTempFolder = Some(tmpFolder) temporaryFileReaper.updateTempFolder(tmpFolder) tmpFolder } @@ -317,24 +319,26 @@ object Files { */ applicationLifecycle.addStopHook { () => Future.successful { - if (JFiles.isDirectory(playTempFolder)) { - JFiles.walkFileTree( - playTempFolder, - new SimpleFileVisitor[Path] { - override def visitFile(path: Path, attrs: BasicFileAttributes): FileVisitResult = { - logger.debug(s"stopHook: Removing leftover temporary file $path from $playTempFolder") - deletePath(path) - FileVisitResult.CONTINUE + _playTempFolder.foreach(playTempFolder => { + if (JFiles.isDirectory(playTempFolder)) { + JFiles.walkFileTree( + playTempFolder, + new SimpleFileVisitor[Path] { + override def visitFile(path: Path, attrs: BasicFileAttributes): FileVisitResult = { + logger.debug(s"stopHook: Removing leftover temporary file $path from $playTempFolder") + deletePath(path) + FileVisitResult.CONTINUE + } + + override def postVisitDirectory(path: Path, exc: IOException): FileVisitResult = { + deletePath(path) + FileVisitResult.CONTINUE + } } - - override def postVisitDirectory(path: Path, exc: IOException): FileVisitResult = { - deletePath(path) - FileVisitResult.CONTINUE - } - } - ) - } - frq.close() + ) + } + frq.close() + }) } } }