Skip to content

Commit

Permalink
PipedProcess.waitForReturnCode: log only when waiting
Browse files Browse the repository at this point in the history
  • Loading branch information
Zschimmer committed Oct 1, 2024
1 parent 54f8116 commit 4713d2a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,26 @@ extends Js7Process:
call("TerminateProcess"):
kernel32.TerminateProcess(hProcess, TerminateProcessReturnCode.number) ||
kernel32.GetLastError == ERROR_ACCESS_DENIED && (
Try(waitForProcess(0)).getOrElse(false) || {
Try(waitForProcess(timeout = 0)).getOrElse(false) || {
kernel32.SetLastError(ERROR_ACCESS_DENIED)
false
})

def waitFor(timeout: FiniteDuration): Boolean =
returnCodeOnce.isDefined ||
waitForProcess(max(0, min(Int.MaxValue, timeout.toMillis)).toInt)
waitForProcess(timeout = max(0, min(Int.MaxValue, timeout.toMillis)).toInt)

def waitFor(): ReturnCode =
returnCodeOnce.getOrElse:
waitForProcess(INFINITE)
waitForProcess(timeout = INFINITE)
returnCodeOnce.orThrow

def returnCode =
returnCodeOnce.toOption
returnCodeOnce.toOption match
case Some(rc) => Some(rc)
case None =>
waitForProcess(timeout = 0)
returnCodeOnce.toOption

/** Must be called to release the hProcess handle. */
private def waitForProcess(timeout: Int): Boolean =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,15 @@ final class PipedProcess private(

val awaitProcessTermination: IO[ReturnCode] =
memoize:
IO.defer:
process.returnCode.map(IO.pure)
.getOrElse:
waitForReturnCode(process)

private def waitForReturnCode(process: Js7Process): IO[ReturnCode] =
// Try onExit to avoid blocking a (virtual) thread
process.maybeHandle.fold(IO.unit)(_.onExitIO) *>
interruptibleVirtualThread:
logger.traceCallWithResult(s"waitFor $process"):
process.waitFor()
process.maybeHandle.fold(IO.unit)(_.onExitIO) *>
IO.defer:
process.returnCode.map(IO.pure)
.getOrElse:
interruptibleVirtualThread:
logger.traceCallWithResult(s"waitFor $process"):
process.waitFor()
.flatTap: rc =>
IO(logger.trace(s"Process $pid terminated with $rc"))

val watchProcessAndStdouterr: IO[ReturnCode] =
memoize:
Expand Down

0 comments on commit 4713d2a

Please sign in to comment.