Skip to content

Commit

Permalink
Merge pull request #6630 from Nirvikalpa108/timings-help-bug
Browse files Browse the repository at this point in the history
  • Loading branch information
eed3si9n committed Aug 21, 2021
2 parents 2a26e81 + 3c9826f commit a199875
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
24 changes: 24 additions & 0 deletions main/src/main/scala/sbt/EvaluateTask.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package sbt

import java.io.File
import java.util.concurrent.atomic.AtomicReference

import sbt.Def.{ ScopedKey, Setting, dummyState }
import sbt.Keys.{ TaskProgress => _, name => _, _ }
import sbt.Project.richInitializeTask
Expand All @@ -28,6 +29,7 @@ import sbt.internal.bsp.BuildTargetIdentifier
import scala.annotation.nowarn
import scala.Console.RED
import scala.concurrent.duration.Duration
import scala.util.control.NonFatal

/**
* An API that allows you to cancel executing tasks upon some signal.
Expand Down Expand Up @@ -169,6 +171,28 @@ object EvaluateTask {
if (SysProp.taskTimingsOnShutdown) Some(sharedProgress)
else None

private val capturedThunk = new AtomicReference[() => Unit]()
def onShutdown(): Unit = {
val thunk = capturedThunk.getAndSet(null)
if (thunk != null) thunk()
}
// our own implementation of shutdown hook, because the "sbt -timings help" command was not working with the JVM shutdown hook,
// which is a little hard to control.
def addShutdownHandler[A](thunk: () => A): Unit = {
capturedThunk
.set(
() =>
try {
thunk()
()
} catch {
case NonFatal(e) =>
System.err.println(s"Caught exception running shutdown hook: $e")
e.printStackTrace(System.err)
}
)
}

lazy private val sharedTraceEvent = new TaskTraceEvent()
def taskTraceEvent: Option[ExecuteProgress[Task]] =
if (SysProp.traces) {
Expand Down
1 change: 1 addition & 0 deletions main/src/main/scala/sbt/internal/CommandExchange.scala
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ private[sbt] final class CommandExchange {
// interrupt and kill the thread
server.foreach(_.shutdown())
server = None
EvaluateTask.onShutdown
}

// This is an interface to directly respond events.
Expand Down
3 changes: 2 additions & 1 deletion main/src/main/scala/sbt/internal/TaskTimings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package sbt
package internal

import sbt.EvaluateTask.addShutdownHandler
import sbt.internal.util.{ ConsoleOut, RMap }
import sbt.util.{ Level, Logger }

Expand Down Expand Up @@ -38,7 +39,7 @@ private[sbt] final class TaskTimings(reportOnShutdown: Boolean, logger: Logger)

if (reportOnShutdown) {
start = System.nanoTime
ShutdownHooks.add(() => report())
addShutdownHandler(() => report())
}

override def initial(): Unit = {
Expand Down

0 comments on commit a199875

Please sign in to comment.