SBT hangs waiting for orphaned subprocess to complete

Deadly 提交于 2019-12-23 13:04:43

问题


In my SBT (0.13.16) build, I have the following task:

startThing := {
  var bin_path = s"${file(".").getAbsolutePath}/bin"
  val result = s"$bin_path/start_thing".!
  if (result != 0)
    throw new RuntimeException("Could not start Thing..")
  true
}

And start_thing contains:

(run_subprocess &)

and my build hangs.

I can see that start_thing exits (the process table does not have it as an entry) but adding some printlns to the task shows that it's stuck on val result = s"$bin_path/start_thing".!.

If I kill the run_subprocess process then SBT unblocks and runs normally.

In this particular case, run_subprocess has set up some Kubernetes port-forwarding that needs to be there in order for subsequent tests to work.


回答1:


Try daemonising the background process like so

(run_subprocess >/dev/null 2>&1 &)

The issue could be output from run_subprocess still going to sbt parent as suggested here.

I was able to replicate the issue in both sbt 0.13.17 and 1.0.2. Daemonising worked in both.




回答2:


Regardless of my comment, in my case the reason for the hanging was ACTUALLY the potential leaving of open STDOUT , STDERR handles in a daemon started by the script , that is OK:

 /usr/local/bin/minio server "$minio_data_dir" > /dev/null 2>&1 &  # and start the server

and NOT ok:

 /usr/local/bin/minio server "$minio_data_dir" 2>&1 &  # and start the server

So the hanging occurred randomly EVEN with the accepted answers start in the background ... Thus this solution needed NOT, any wrapper bash scripts ... This is how the code looked in the build.sbt

lazy val startLocalS3 = inputKey[Unit]("localS3")
lazy val startLocalS3Task = TaskKey[Unit]("localS3", "create local s3")
lazy val core: Project = project
   .in(file("."))
   .settings(
      name := "rfco-core",
      startLocalS3Task := {
      val cmd: Seq[String] = Seq("bash" , "-c" , "./CI/start-s3-svr.sh")
      import sys.process._
      cmd.mkString(" ").!!
     },
     fork in startLocalS3Task := true,   
     compile.in(Compile) := (compile in Compile).dependsOn((startLocalS3Task)).value
     // you might want to use Test scope ^^ here 
 )


来源:https://stackoverflow.com/questions/50743437/sbt-hangs-waiting-for-orphaned-subprocess-to-complete

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!