Skip to content

Commit

Permalink
Docker: improved posix signal proxying
Browse files Browse the repository at this point in the history
fixes #547
  • Loading branch information
borsboom committed Nov 30, 2015
1 parent 159c10a commit 52944d2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
32 changes: 20 additions & 12 deletions src/Stack/Docker.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Stack.Docker
) where

import Control.Applicative
import Control.Concurrent (threadDelay)
import Control.Concurrent.MVar.Lifted (MVar,modifyMVar_,newMVar)
import Control.Exception.Lifted
import Control.Monad
Expand Down Expand Up @@ -330,10 +331,14 @@ runContainerAndExit getCmdArgs
before
#ifndef WINDOWS
runInBase <- liftBaseWith $ \run -> return (void . run)
oldHandlers <- forM ([sigINT | not keepStdinOpen] ++ [sigTERM]) $ \sig -> do
let sigHandler = do
runInBase (readProcessNull Nothing envOverride "docker"
["kill","--signal=" ++ show sig,containerID])
oldHandlers <- forM ([sigINT,sigABRT,sigHUP,sigPIPE,sigTERM,sigUSR1,sigUSR2]) $ \sig -> do
let sigHandler = runInBase $ do
readProcessNull Nothing envOverride "docker"
["kill","--signal=" ++ show sig,containerID]
when (sig `elem` [sigTERM,sigABRT]) $ do
-- Give the container 30 seconds to exit gracefully, then send a sigKILL to force it
liftIO $ threadDelay 30000000
readProcessNull Nothing envOverride "docker" ["kill",containerID]
oldHandler <- liftIO $ installHandler sig (Catch sigHandler) Nothing
return (sig, oldHandler)
#endif
Expand All @@ -344,16 +349,19 @@ runContainerAndExit getCmdArgs
,["-a" | not (dockerDetach docker)]
,["-i" | keepStdinOpen]
,[containerID]])
e <- try (callProcess'
(if keepStdinOpen then id else (\cp -> cp { delegate_ctlc = False }))
cmd
)
e <- finally
(try $ callProcess'
(\cp -> cp { delegate_ctlc = False })
cmd)
(do unless (dockerPersist docker || dockerDetach docker) $
catch
(readProcessNull Nothing envOverride "docker" ["rm","-f",containerID])
(\(_::ReadProcessException) -> return ())
#ifndef WINDOWS
forM_ oldHandlers $ \(sig,oldHandler) ->
liftIO $ installHandler sig oldHandler Nothing
forM_ oldHandlers $ \(sig,oldHandler) ->
liftIO $ installHandler sig oldHandler Nothing
#endif
unless (dockerPersist docker || dockerDetach docker)
(readProcessNull Nothing envOverride "docker" ["rm","-f",containerID])
)
case e of
Left (ProcessExitedUnsuccessfully _ ec) -> liftIO (exitWith ec)
Right () -> do after
Expand Down
2 changes: 1 addition & 1 deletion src/System/Process/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ tryProcessStdout wd menv name args =

-- | Produce a strict 'S.ByteString' from the stdout of a process.
--
-- Throws a 'ProcessExitedUnsuccessfully' exception if the process fails.
-- Throws a 'ReadProcessException' exception if the process fails.
readProcessStdout :: (MonadIO m, MonadLogger m, MonadBaseControl IO m, MonadCatch m)
=> Maybe (Path Abs Dir) -- ^ Optional directory to run in
-> EnvOverride
Expand Down

0 comments on commit 52944d2

Please sign in to comment.