Throw timeoutexception in JoinThread

Rather than throwing an interrupted exception if we fail to join a
thread, we should throw a timeout exception.
This commit is contained in:
Ethan Atkins 2020-11-05 11:09:00 -08:00
parent e5164cdf43
commit 748dfd6e67
1 changed files with 3 additions and 4 deletions

View File

@ -9,23 +9,22 @@ package sbt.internal.util
import scala.annotation.tailrec
import scala.concurrent.duration._
import java.util.concurrent.TimeoutException
object JoinThread {
implicit class ThreadOps(val t: Thread) extends AnyVal {
def joinFor(duration: FiniteDuration): Unit = {
val deadline = duration.fromNow
var exception: Option[InterruptedException] = None
@tailrec def impl(): Unit = {
try {
t.interrupt()
t.join(10)
} catch { case e: InterruptedException => exception = Some(e) }
} catch { case e: InterruptedException => }
if (t.isAlive && !deadline.isOverdue) impl()
}
impl()
if (t.isAlive) {
System.err.println(s"Unable to join thread $t after $duration")
exception.foreach(throw _)
throw new TimeoutException(s"Unable to join thread $t after $duration")
}
}
}