From 53aa642e2be2e8fdfc9fffd8d4da6f2fa23c53ee Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Fri, 30 Oct 2026 19:04:09 +0100 Subject: [PATCH 1/2] bugfix: Try to fix concurrency issues with cats --- .../src/main/scala/scoverage/Platform.scala | 4 + .../src/main/scala/scoverage/Platform.scala | 18 ++++ .../scoverage/InvokerConcurrencyTest.scala | 27 +++++ .../src/main/scala/scoverage/Platform.scala | 18 ++++ .../src/main/scala/scoverage/Invoker.scala | 99 +++++++++++++++++-- 5 files changed, 157 insertions(+), 9 deletions(-) diff --git a/runtime/js/src/main/scala/scoverage/Platform.scala b/runtime/js/src/main/scala/scoverage/Platform.scala index 70f7bb07..9b326b98 100644 --- a/runtime/js/src/main/scala/scoverage/Platform.scala +++ b/runtime/js/src/main/scala/scoverage/Platform.scala @@ -28,4 +28,8 @@ object Platform { new java.util.UUID(msb, lsb) } + // Scala.js does not run JVM shutdown hooks. Measurement files are still + // written from `invoked`, and `dumpRecordedIds` can be called directly. + def onShutdown(hook: () => Unit): Unit = () + } diff --git a/runtime/jvm/src/main/scala/scoverage/Platform.scala b/runtime/jvm/src/main/scala/scoverage/Platform.scala index f8e66d43..63e15924 100644 --- a/runtime/jvm/src/main/scala/scoverage/Platform.scala +++ b/runtime/jvm/src/main/scala/scoverage/Platform.scala @@ -6,6 +6,7 @@ import java.io.{FileWriter => SupportFileWriter} import scala.collection.concurrent.TrieMap import scala.io.{Source => SupportSource} +import scala.util.control.NonFatal object Platform { type ThreadSafeMap[A, B] = TrieMap[A, B] @@ -19,4 +20,21 @@ object Platform { def insecureRandomUUID() = java.util.UUID.randomUUID() + def onShutdown(hook: () => Unit): Unit = { + val thread = new Thread(new Runnable { + override def run(): Unit = + try hook() + catch { case NonFatal(_) => () } + }) + thread.setName("scoverage-measurement-dump") + try Runtime.getRuntime.addShutdownHook(thread) + catch { + case _: IllegalStateException => + try hook() + catch { case NonFatal(_) => () } + case _: SecurityException => + () + } + } + } diff --git a/runtime/jvm/src/test/scala/scoverage/InvokerConcurrencyTest.scala b/runtime/jvm/src/test/scala/scoverage/InvokerConcurrencyTest.scala index d2f3df2a..0d9ac8ef 100644 --- a/runtime/jvm/src/test/scala/scoverage/InvokerConcurrencyTest.scala +++ b/runtime/jvm/src/test/scala/scoverage/InvokerConcurrencyTest.scala @@ -45,6 +45,33 @@ class InvokerConcurrencyTest extends FunSuite { assertEquals(idsFromFile, testIds) } + test("shutdown-snapshot-keeps-ids-recorded-from-many-threads") { + val dir = new File("target/invoker-test-snapshot.measurement") + if (dir.isDirectory()) dir.listFiles().foreach(_.delete()) + dir.mkdirs() + + val testIds: Set[Int] = (1 to 1000).toSet + val futures: Set[Future[Unit]] = testIds.map { i: Int => + Future { + Invoker.invoked(i, dir.getAbsolutePath()) + } + } + futures.foreach(Await.result(_, 3.second)) + + Invoker.findMeasurementFiles(dir).foreach(_.delete()) + Invoker.dumpRecordedIds() + + val shutdownFiles = + Invoker + .findMeasurementFiles(dir) + .filter(_.getName().endsWith(".shutdown")) + assertEquals(shutdownFiles.length, 1) + assertEquals(Invoker.invoked(shutdownFiles.toIndexedSeq), testIds) + + dir.listFiles().foreach(_.delete()) + dir.delete() + } + override def afterAll(): Unit = { deleteMeasurementFiles() measurementDir.delete() diff --git a/runtime/native/src/main/scala/scoverage/Platform.scala b/runtime/native/src/main/scala/scoverage/Platform.scala index fa3902d3..da640f2a 100644 --- a/runtime/native/src/main/scala/scoverage/Platform.scala +++ b/runtime/native/src/main/scala/scoverage/Platform.scala @@ -6,6 +6,7 @@ import java.io.{FileWriter => SupportFileWriter} import scala.collection.mutable.HashMap import scala.io.{Source => SupportSource} +import scala.util.control.NonFatal object Platform { type ThreadSafeMap[A, B] = HashMap[A, B] @@ -28,4 +29,21 @@ object Platform { new java.util.UUID(msb, lsb) } + def onShutdown(hook: () => Unit): Unit = { + val thread = new Thread(new Runnable { + override def run(): Unit = + try hook() + catch { case NonFatal(_) => () } + }) + thread.setName("scoverage-measurement-dump") + try Runtime.getRuntime.addShutdownHook(thread) + catch { + case _: IllegalStateException => + try hook() + catch { case NonFatal(_) => () } + case _: SecurityException => + () + } + } + } diff --git a/runtime/shared/src/main/scala/scoverage/Invoker.scala b/runtime/shared/src/main/scala/scoverage/Invoker.scala index f7615137..35e56243 100644 --- a/runtime/shared/src/main/scala/scoverage/Invoker.scala +++ b/runtime/shared/src/main/scala/scoverage/Invoker.scala @@ -2,6 +2,7 @@ package scoverage import scala.collection.Set import scala.collection.mutable +import scala.util.control.NonFatal import scoverage.Platform._ @@ -12,18 +13,31 @@ object Invoker { private val MeasurementsPrefix = "scoverage.measurements." private val threadFiles = new ThreadLocal[mutable.HashMap[String, FileWriter]] + private val threadMeasurementFiles = + new ThreadLocal[mutable.HashMap[String, File]] // For each data directory we maintain a thread-safe set tracking the ids that we've already - // seen and recorded. We're using a map as a set, so we only care about its keys and can ignore - // its values. + // seen and recorded. We're using a map as a set. The value is the recorded test name, or "" + // when test names are not reported. private val dataDirToIds = ThreadSafeMap.empty[String, ThreadSafeMap[Int, Any]] + private var measurementFileCounter = 0L + + @volatile private var shutdownHookInstalled = false + /** We record that the given id has been invoked by appending its id to the coverage * data file. * * This will happen concurrently on as many threads as the application is using, - * so we use one file per thread, named for the thread id. + * so we use one file per thread. The name includes a counter as well as the thread + * id: thread ids are reused after a thread dies, and a second [[FileWriter]] opened + * on the same path can drop or corrupt ids already flushed by the previous thread. + * Because each id is written at most once, that loss cannot be repaired by a later call. + * + * The in-memory id set is therefore also written to + * `scoverage.measurements..shutdown` from a shutdown hook. The reporter unions + * every measurement file, and duplicate ids do not change coverage. * * This method is not thread-safe if the threads are in different JVMs, because * the thread IDs may collide. @@ -38,6 +52,7 @@ object Invoker { dataDir: String, reportTestName: Boolean = false ): Unit = { + ensureShutdownHook() // [sam] we can do this simple check to save writing out to a file. // This won't work across JVMs but since there's no harm in writing out the same id multiple // times since for coverage we only care about 1 or more, (it just slows things down to @@ -64,19 +79,67 @@ object Invoker { dataDir, new FileWriter(measurementFile(dataDir), true) ) + val testName = if (reportTestName) getCallingScalaTest else "" if (reportTestName) writer .append(Integer.toString(id)) .append(" ") - .append(getCallingScalaTest) + .append(testName) .append("\n") .flush() else writer.append(Integer.toString(id)).append("\n").flush() - ids.put(id, ()) + ids.put(id, testName) } } + private def ensureShutdownHook(): Unit = + if (!shutdownHookInstalled) { + synchronized { + if (!shutdownHookInstalled) { + Platform.onShutdown(() => dumpRecordedIds()) + shutdownHookInstalled = true + } + } + } + + /** Write every id recorded in memory into a measurement file. + * + * Per-thread files are an optimization. This snapshot is what the reporter + * can trust after the instrumented JVM exits: forked tests (the usual sbt + * `coverage` / `test` / `coverageReport` sequence) read measurement files + * only once that process has shut down. + */ + private[scoverage] def dumpRecordedIds(): Unit = + dataDirToIds.foreach { case (dataDir, ids) => + try { + val dir = new File(dataDir) + if (dir.isDirectory() && ids.nonEmpty) { + val content = new mutable.StringBuilder + ids.foreach { case (id, rawName) => + content.append(Integer.toString(id)) + rawName match { + case name: String if name.nonEmpty => + content.append(" ").append(name) + case _ => + } + content.append('\n') + } + val writer = new FileWriter( + new File( + dataDir, + MeasurementsPrefix + runtimeUUID + ".shutdown" + ), + true + ) + try writer.append(content).flush() + finally writer.close() + } + } catch { + case NonFatal(_) => () + } + } + def getCallingScalaTest: String = Thread.currentThread.getStackTrace .map(_.getClassName.toLowerCase) @@ -88,10 +151,28 @@ object Invoker { def measurementFile(dataDir: File): File = measurementFile( dataDir.getAbsolutePath() ) - def measurementFile(dataDir: String): File = new File( - dataDir, - MeasurementsPrefix + runtimeUUID + "." + Thread.currentThread.getId - ) + def measurementFile(dataDir: String): File = { + var files = threadMeasurementFiles.get() + if (files == null) { + files = mutable.HashMap.empty[String, File] + threadMeasurementFiles.set(files) + } + files.getOrElseUpdate(dataDir, allocateMeasurementFile(dataDir)) + } + + // Thread ids are reused after a thread terminates. Keep a unique suffix so a + // new thread never opens a FileWriter on a file another thread still has open. + private def allocateMeasurementFile(dataDir: String): File = { + val unique = synchronized { + measurementFileCounter += 1 + measurementFileCounter + } + new File( + dataDir, + MeasurementsPrefix + runtimeUUID + "." + + Thread.currentThread.getId + "." + unique + ) + } def findMeasurementFiles(dataDir: String): Array[File] = findMeasurementFiles( new File(dataDir) From 77ce602b2380539bf55f6ba9a634fc7406d25f4b Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Fri, 30 Oct 2026 19:14:42 +0100 Subject: [PATCH 2/2] refactor: Simplify aproach --- .../src/main/scala/scoverage/Invoker.scala | 52 ++++++------------- 1 file changed, 17 insertions(+), 35 deletions(-) diff --git a/runtime/shared/src/main/scala/scoverage/Invoker.scala b/runtime/shared/src/main/scala/scoverage/Invoker.scala index 35e56243..36f83988 100644 --- a/runtime/shared/src/main/scala/scoverage/Invoker.scala +++ b/runtime/shared/src/main/scala/scoverage/Invoker.scala @@ -12,9 +12,8 @@ object Invoker { private val runtimeUUID = Platform.insecureRandomUUID() private val MeasurementsPrefix = "scoverage.measurements." - private val threadFiles = new ThreadLocal[mutable.HashMap[String, FileWriter]] - private val threadMeasurementFiles = - new ThreadLocal[mutable.HashMap[String, File]] + private val threadFiles = + new ThreadLocal[mutable.HashMap[String, FileWriter]] // For each data directory we maintain a thread-safe set tracking the ids that we've already // seen and recorded. We're using a map as a set. The value is the recorded test name, or "" @@ -22,18 +21,15 @@ object Invoker { private val dataDirToIds = ThreadSafeMap.empty[String, ThreadSafeMap[Int, Any]] - private var measurementFileCounter = 0L - @volatile private var shutdownHookInstalled = false /** We record that the given id has been invoked by appending its id to the coverage * data file. * * This will happen concurrently on as many threads as the application is using, - * so we use one file per thread. The name includes a counter as well as the thread - * id: thread ids are reused after a thread dies, and a second [[FileWriter]] opened - * on the same path can drop or corrupt ids already flushed by the previous thread. - * Because each id is written at most once, that loss cannot be repaired by a later call. + * so we use one file per thread, named for the thread id. + * Because each id is written at most once, a lost per-thread write cannot be + * repaired by a later call. * * The in-memory id set is therefore also written to * `scoverage.measurements..shutdown` from a shutdown hook. The reporter unions @@ -70,15 +66,7 @@ object Invoker { if (!ids.contains(id)) { // Each thread writes to a separate measurement file, to reduce contention // and because file appends via FileWriter are not atomic on Windows. - var files = threadFiles.get() - if (files == null) { - files = mutable.HashMap.empty[String, FileWriter] - threadFiles.set(files) - } - val writer = files.getOrElseUpdate( - dataDir, - new FileWriter(measurementFile(dataDir), true) - ) + val writer = writerFor(dataDir) val testName = if (reportTestName) getCallingScalaTest else "" if (reportTestName) @@ -151,26 +139,20 @@ object Invoker { def measurementFile(dataDir: File): File = measurementFile( dataDir.getAbsolutePath() ) - def measurementFile(dataDir: String): File = { - var files = threadMeasurementFiles.get() - if (files == null) { - files = mutable.HashMap.empty[String, File] - threadMeasurementFiles.set(files) - } - files.getOrElseUpdate(dataDir, allocateMeasurementFile(dataDir)) - } + def measurementFile(dataDir: String): File = new File( + dataDir, + MeasurementsPrefix + runtimeUUID + "." + Thread.currentThread.getId + ) - // Thread ids are reused after a thread terminates. Keep a unique suffix so a - // new thread never opens a FileWriter on a file another thread still has open. - private def allocateMeasurementFile(dataDir: String): File = { - val unique = synchronized { - measurementFileCounter += 1 - measurementFileCounter + private def writerFor(dataDir: String): FileWriter = { + var files = threadFiles.get() + if (files == null) { + files = mutable.HashMap.empty[String, FileWriter] + threadFiles.set(files) } - new File( + files.getOrElseUpdate( dataDir, - MeasurementsPrefix + runtimeUUID + "." + - Thread.currentThread.getId + "." + unique + new FileWriter(measurementFile(dataDir), true) ) }