Cleanup cache when CommandExchange shuts down

I noticed that sometimes when running scripted tests that I'd run out of
metaspace. I believe that this may be due to the caffeine cache leaking
classloaders. Regardless, it's a good idea to clear the cache whenever
we shutdown the command exchange or reload the state.
This commit is contained in:
Ethan Atkins 2019-01-13 15:18:30 -08:00
parent 6e5caa90c1
commit 06a153278f
1 changed files with 11 additions and 8 deletions

View File

@ -16,17 +16,18 @@ import sbt.Project.LoadAction
import sbt.compiler.EvalImports
import sbt.internal.Aggregation.AnyKeys
import sbt.internal.CommandStrings.BootCommand
import sbt.internal._
import sbt.internal.inc.ScalaInstance
import sbt.internal.util.Types.{ const, idFun }
import sbt.internal.util.complete.Parser
import sbt.internal.util._
import sbt.internal._
import sbt.internal.util.complete.Parser
import sbt.io.syntax._
import sbt.io.{ FileTreeDataView, IO }
import sbt.util.{ Level, Logger, Show }
import xsbti.compile.CompilerCache
import scala.annotation.tailrec
import scala.concurrent.ExecutionContext
import scala.util.control.NonFatal
/** This class is the entry point for sbt. */
@ -85,13 +86,15 @@ final class ConsoleMain extends xsbti.AppMain {
object StandardMain {
private[sbt] lazy val exchange = new CommandExchange()
import scalacache._
import scalacache.caffeine._
private[sbt] lazy val cache: Cache[Any] = CaffeineCache[Any]
private[sbt] lazy val cache: scalacache.Cache[Any] = CaffeineCache[Any]
private[sbt] val shutdownHook = new Thread(() => {
exchange.shutdown
})
private[this] val closeRunnable: Runnable = () => {
cache.close()(scalacache.modes.sync.mode)
cache.close()(scalacache.modes.scalaFuture.mode(ExecutionContext.global))
exchange.shutdown()
}
private[sbt] val shutdownHook = new Thread(closeRunnable)
def runManaged(s: State): xsbti.MainResult = {
val previous = TrapExit.installManager()
@ -106,7 +109,7 @@ object StandardMain {
try {
MainLoop.runLogged(s)
} finally {
exchange.shutdown
closeRunnable.run()
if (hooked) {
Runtime.getRuntime.removeShutdownHook(shutdownHook)
}