Skip to content

Commit d410dee

Browse files
committed
changed fallbackexitval
1 parent f1202f3 commit d410dee

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

io/native/src/main/scala/fs2/io/process/ProcessesPlatform.scala

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ import java.io.IOException
3737
import cats.effect.LiftIO
3838
import cats.effect.IO
3939
import org.typelevel.scalaccompat.annotation._
40+
import scala.concurrent.duration.*
41+
import cats.effect.implicits.*
4042

4143
@extern
4244
@nowarn212("cat=unused")
@@ -247,13 +249,30 @@ private[process] trait ProcessesCompanionPlatform extends Processesjvmnative {
247249
}
248250
}
249251

250-
private def fallbackExitValue(pid: pid_t): F[Int] =
251-
F.blocking {
252-
val status = stackalloc[CInt]()
253-
val result = waitpid(pid, status, 0)
254-
if (result == pid) WEXITSTATUS(!status)
255-
else throw new IOException(s"waitpid failed with errno: ${errno.errno}")
252+
private def fallbackExitValue(pid: pid_t): F[Int] = {
253+
def loop: F[Int] =
254+
F.blocking {
255+
Zone { _ =>
256+
val status = stackalloc[CInt]()
257+
val result = waitpid(pid, status, WNOHANG)
258+
259+
if (result == pid) {
260+
Some(WEXITSTATUS(!status))
261+
} else if (result == 0) None
262+
else throw new IOException(s"waitpid failed with errno: ${errno.errno}")
263+
}
264+
}.flatMap {
265+
case Some(code) => F.pure(code)
266+
case None => F.sleep(10.millis) >> loop
267+
}
268+
269+
loop.onCancel {
270+
F.blocking {
271+
kill(pid, SIGKILL)
272+
()
273+
}
256274
}
275+
}
257276
}
258277
} else super.forAsync[F]
259278
}

0 commit comments

Comments
 (0)