Skip to content

Commit 1912628

Browse files
committed
Fix cleanup when stdin/stdout handles are inherited and are Nothing
1 parent c1ce40e commit 1912628

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

src/Streamly/Internal/System/Process.hs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ cleanupNormal (_, _, _, procHandle) = do
518518
-- still hanging around.
519519
cleanupException ::
520520
(Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) -> IO ()
521-
cleanupException (Just stdinH, Just stdoutH, stderrMaybe, ph) = do
521+
cleanupException (stdinMaybe, stdoutMaybe, stderrMaybe, ph) = do
522522
-- Send a SIGTERM to the process
523523
#ifdef USE_NATIVE
524524
terminate ph
@@ -529,8 +529,8 @@ cleanupException (Just stdinH, Just stdoutH, stderrMaybe, ph) = do
529529
-- Ideally we should be closing the handle without flushing the buffers so
530530
-- that we cannot get a SIGPIPE. But there seems to be no way to do that as
531531
-- of now so we just ignore the SIGPIPE.
532-
hClose stdinH `catch` eatSIGPIPE
533-
hClose stdoutH
532+
whenJust (\stdinH -> hClose stdinH `catch` eatSIGPIPE) stdinMaybe
533+
whenJust hClose stdoutMaybe
534534
whenJust hClose stderrMaybe
535535

536536
-- Non-blocking wait for the process to go away
@@ -553,7 +553,6 @@ cleanupException (Just stdinH, Just stdoutH, stderrMaybe, ph) = do
553553
_ -> False
554554

555555
eatSIGPIPE e = unless (isSIGPIPE e) $ throwIO e
556-
cleanupException _ = error "cleanupProcess: Not reachable"
557556

558557
-- | Creates a system process from an executable path and arguments. For the
559558
-- default attributes used to create the process see 'mkConfig'.
@@ -633,11 +632,10 @@ pipeChunksEitherWith modifier path args input =
633632

634633
where
635634

636-
run (Just stdinH, Just stdoutH, Just stderrH, _) =
637-
putChunksClose stdinH input
638-
`parallel` fmap Left (toChunksClose stderrH)
639-
`parallel` fmap Right (toChunksClose stdoutH)
640-
run _ = error "pipeChunksEitherWith: Not reachable"
635+
run (stdinMaybe, stdoutMaybe, stderrMaybe, _) =
636+
whenJustS (`putChunksClose` input) stdinMaybe
637+
`parallel` fmap Left (whenJustS toChunksClose stderrMaybe)
638+
`parallel` fmap Right (whenJustS toChunksClose stdoutMaybe)
641639

642640
-- | Like 'pipeChunks' but also includes stderr as 'Left' stream in the
643641
-- 'Either' output.
@@ -706,9 +704,9 @@ pipeChunksWith modifier path args input =
706704

707705
where
708706

709-
run (Just stdinH, Just stdoutH, _, _) =
710-
putChunksClose stdinH input `parallel` toChunksClose stdoutH
711-
run _ = error "pipeChunksWith: Not reachable"
707+
run (stdinMaybe, stdoutMaybe, _, _) =
708+
whenJustS (`putChunksClose` input) stdinMaybe
709+
`parallel` whenJustS toChunksClose stdoutMaybe
712710

713711
-- | @pipeChunks file args input@ runs the executable @file@ specified by
714712
-- its name or path using @args@ as arguments and @input@ stream as its
@@ -855,10 +853,9 @@ toChunksEitherWith modifier path args =
855853

856854
where
857855

858-
run (_, Just stdoutH, Just stderrH, _) =
859-
fmap Left (toChunksClose stderrH)
860-
`parallel` fmap Right (toChunksClose stdoutH)
861-
run _ = error "toChunksEitherWith: Not reachable"
856+
run (_, stdoutMaybe, stderrMaybe, _) =
857+
fmap Left (whenJustS toChunksClose stderrMaybe)
858+
`parallel` fmap Right (whenJustS toChunksClose stdoutMaybe)
862859

863860
-- | Like 'toChunks' but use the specified configuration to run the process.
864861
{-# INLINE toChunksWith #-}
@@ -873,8 +870,7 @@ toChunksWith modifier path args =
873870

874871
where
875872

876-
run (_, Just stdoutH, _, _) = toChunksClose stdoutH
877-
run _ = error "toChunksWith: Not reachable"
873+
run (_, stdoutMaybe, _, _) = whenJustS toChunksClose stdoutMaybe
878874

879875
-- | @toBytesEither path args@ runs the executable at @path@ using @args@ as
880876
-- arguments and returns the output of the process as a stream of 'Either'
@@ -1181,3 +1177,7 @@ daemon modCfg path args =
11811177
(setSession NewSession . modCfg)
11821178
path args
11831179
in fmap (either undefined id) r
1180+
1181+
1182+
whenJustS :: Applicative m => (a -> Stream m b) -> Maybe a -> Stream m b
1183+
whenJustS action mb = maybe Stream.nil action mb

0 commit comments

Comments
 (0)