process
process copied to clipboard
WIP: make waitForProcess async safe
Moving from https://github.com/fpco/typed-process/issues/38. CC @norfairking
This PR is not complete, the tests fail. I'm trying to determine whether this is a reasonable approach. My understanding of the issue here:
-
waitForProcess
wants to allow the C FFI callc_waitForProcess
to be interruptible, so thatwaitpid
can be interrupted - If an async exception lands at exactly the wrong time, the
waitpid
call with succeed, but the FFI call will throw an exception due to the async exception - In this case, the system process table no longer has an entry for the given PID, but the
MVar
insideProcessHandle
believes that the handle is still open, allowing futurewaitForProcess
calls to occur - Future calls throw an exception, since it's using an unknown PID
My best guess is that, in order to both allow waitpid
to be interrupted and to reliably capture the exit code if it's generated, we need to:
- No longer rely on return codes from
c_waitForProcess
, since those will be lost in the case of an async exception - Therefore, we need to detect whether the
waitpid
call succeeded or not by passing in anotherint*
(along with the existing output parameterpret
). - Even if
c_waitForProcess
throws an exception, we need to update theMVar
with the exit code, if it's returned successfully
I've taken a stab at this in this PR. @simonmar I was hoping you could have a look and let me know if this approach works, and/or if there's a better way to attack this kind of problem.
@snoyberg Thanks for CC-ing me. I don't feel qualified to review this PR, to be honest. I'm betting that @nh2 will, if he has some spare cycles to help with this.
I thought a bit about it and will try to reply to reply tomorrow.
@simonmar Gentle ping about this.