Skip to content

Commit

Permalink
Added code to acquire write lock on index.html first before writing i…
Browse files Browse the repository at this point in the history
…t, to avoid concurrent write to index.html.
  • Loading branch information
cheeseng committed Sep 14, 2023
1 parent e32c9e6 commit 1fb65c7
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,9 @@ private[scalatest] class HtmlReporter(
}

private def makeIndexFile(completeMessageFun: => String, completeInMessageFun: String => String, duration: Option[Long]): Unit = {
val pw = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(new File(targetDir, "index.html")), BufferSize), "UTF-8"))
val fos = new FileOutputStream(new File(targetDir, "index.html"))
val lock = fos.getChannel().lock()
val pw = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(fos, BufferSize), "UTF-8"))
try {
pw.println {
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
Expand All @@ -463,6 +465,8 @@ private[scalatest] class HtmlReporter(
finally {
pw.flush()
pw.close()
if (lock.isValid) // release lock if it is still valid
lock.release()
}
}

Expand Down

0 comments on commit 1fb65c7

Please sign in to comment.