From 14dee32d6b8336e50828dec5dcbfe6444ca04bab Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Thu, 19 Nov 2020 22:20:12 -0800 Subject: [PATCH] Stop CI hangs in background job service shutdown A periodic stacktrace showed that scripted tests were still hanging in ci trying to shutdown the background job service (I had previously thought that I'd fixed that in 16bef0cfc8e9018a2a7373f307d9e00d7bdfb905). It appears that there is a logical bug that prevents some jobs from being removed from the jobSet even though they have finished. If that happens, the shutdown will never exit. That is highly undesirable and can be avoided by adding a timeout and also only trying to shutdown the job if it is actually running. --- .../sbt/internal/DefaultBackgroundJobService.scala | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/main/src/main/scala/sbt/internal/DefaultBackgroundJobService.scala b/main/src/main/scala/sbt/internal/DefaultBackgroundJobService.scala index 21a3615fd..fa16e888d 100644 --- a/main/src/main/scala/sbt/internal/DefaultBackgroundJobService.scala +++ b/main/src/main/scala/sbt/internal/DefaultBackgroundJobService.scala @@ -179,11 +179,15 @@ private[sbt] abstract class AbstractBackgroundJobService extends BackgroundJobSe override final def close(): Unit = shutdown() override def shutdown(): Unit = { - while (jobSet.nonEmpty) { + val deadline = 10.seconds.fromNow + while (jobSet.nonEmpty && !deadline.isOverdue) { jobSet.headOption.foreach { case handle: ThreadJobHandle @unchecked => - handle.job.shutdown() - handle.job.awaitTerminationTry(10.seconds) + if (handle.job.isRunning) { + handle.job.shutdown() + handle.job.awaitTerminationTry(10.seconds) + } + jobSet = jobSet - handle case _ => // } }