Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix shutdown hook failures in tests #422

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions main/core/src/mill/util/ClassLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,7 @@ import io.github.retronym.java9rtexport.Export
object ClassLoader {

def create(urls: Seq[URL], parent: java.lang.ClassLoader)(
implicit ctx: Ctx.Home): URLClassLoader = {
new URLClassLoader(
makeUrls(urls).toArray,
refinePlatformParent(parent)
) {
override def findClass(name: String): Class[_] = {
if (name.startsWith("com.sun.jna")) getClass.getClassLoader.loadClass(name)
else super.findClass(name)
}
}
}
implicit ctx: Ctx.Home): URLClassLoader = create(urls, parent, _ => None)

def create(urls: Seq[URL],
parent: java.lang.ClassLoader,
Expand Down
17 changes: 10 additions & 7 deletions main/src/mill/modules/Jvm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ object Jvm {
classPath: Agg[Path],
mainArgs: Seq[String] = Seq.empty)
(implicit ctx: Ctx): Unit = {
inprocess(classPath, classLoaderOverrideSbtTesting = false, isolated = true, cl => {
inprocess(classPath, classLoaderOverrideSbtTesting = false, isolated = true, closeContextClassLoaderWhenDone = true, cl => {
getMainMethod(mainClass, cl).invoke(null, mainArgs.toArray)
})
}
Expand All @@ -113,6 +113,7 @@ object Jvm {
def inprocess[T](classPath: Agg[Path],
classLoaderOverrideSbtTesting: Boolean,
isolated: Boolean,
closeContextClassLoaderWhenDone: Boolean,
body: ClassLoader => T)
(implicit ctx: Ctx.Home): T = {
val urls = classPath.map(_.toIO.toURI.toURL)
Expand All @@ -123,19 +124,21 @@ object Jvm {
Some(outerClassLoader.loadClass(name))
else None
})
} else if (isolated){

} else if (isolated) {
mill.util.ClassLoader.create(urls.toVector, null)
}else{
} else {
mill.util.ClassLoader.create(urls.toVector, getClass.getClassLoader)
}

val oldCl = Thread.currentThread().getContextClassLoader
Thread.currentThread().setContextClassLoader(cl)
try {
body(cl)
}finally{
Thread.currentThread().setContextClassLoader(oldCl)
cl.close()
} finally {
if (closeContextClassLoaderWhenDone) {
Thread.currentThread().setContextClassLoader(oldCl)
cl.close()
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion scalalib/src/mill/scalalib/TestRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ object TestRunner {
testClassfilePath: Agg[Path],
args: Seq[String])
(implicit ctx: Ctx.Log with Ctx.Home): (String, Seq[mill.scalalib.TestRunner.Result]) = {
Jvm.inprocess(entireClasspath, classLoaderOverrideSbtTesting = true, isolated = true, cl => {
//Leave the context class loader set and open so that shutdown hooks can access it
Jvm.inprocess(entireClasspath, classLoaderOverrideSbtTesting = true, isolated = true, closeContextClassLoaderWhenDone = false, cl => {
val frameworks = frameworkInstances(cl)

val events = mutable.Buffer.empty[Event]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ trait TestScalaNativeModule extends ScalaNativeModule with TestModule { testOute
val frameworkInstances = TestRunner.frameworks(testFrameworks()) _

val testClasses =
Jvm.inprocess(testClasspathJvm().map(_.path), classLoaderOverrideSbtTesting = true, isolated = true,
Jvm.inprocess(testClasspathJvm().map(_.path), classLoaderOverrideSbtTesting = true, isolated = true, closeContextClassLoaderWhenDone = true,
cl => {
frameworkInstances(cl).flatMap { framework =>
val df = Lib.discoverTests(cl, framework, Agg(compile().classes.path))
Expand Down