Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull out terminateProcess function #72

Merged
merged 3 commits into from
May 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions src/System/Process/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -256,32 +256,35 @@ startProcess pConfig'@ProcessConfig {..} = liftIO $ do
-- Process didn't exit yet, let's terminate it and
-- then call waitForProcess ourselves
Left _ -> do
eres <- try $ P.terminateProcess pHandle
ec <-
case eres of
Left e
-- On Windows, with the single-threaded runtime, it
-- seems that if a process has already exited, the
-- call to terminateProcess will fail with a
-- permission denied error. To work around this, we
-- catch this exception and then immediately
-- waitForProcess. There's a chance that there may be
-- other reasons for this permission error to appear,
-- in which case this code may allow us to wait too
-- long for a child process instead of erroring out.
-- Recommendation: always use the multi-threaded
-- runtime!
| isPermissionError e && not multiThreadedRuntime && isWindows ->
P.waitForProcess pHandle
| otherwise -> throwIO e
Right () -> P.waitForProcess pHandle
terminateProcess pHandle
ec <- P.waitForProcess pHandle
success <- atomically $ tryPutTMVar pExitCode ec
evaluate $ assert success ()

return Process {..}
where
pConfig = clearStreams pConfig'

terminateProcess pHandle = do
eres <- try $ P.terminateProcess pHandle
case eres of
Left e
-- On Windows, with the single-threaded runtime, it
-- seems that if a process has already exited, the
-- call to terminateProcess will fail with a
-- permission denied error. To work around this, we
-- catch this exception and then immediately
-- waitForProcess. There's a chance that there may be
-- other reasons for this permission error to appear,
-- in which case this code may allow us to wait too
-- long for a child process instead of erroring out.
-- Recommendation: always use the multi-threaded
-- runtime!
| isPermissionError e && not multiThreadedRuntime && isWindows ->
pure ()
| otherwise -> throwIO e
Right () -> pure ()

foreign import ccall unsafe "rtsSupportsBoundThreads"
multiThreadedRuntime :: Bool

Expand Down