@@ -19,6 +19,7 @@ module Stack.Docker
19
19
) where
20
20
21
21
import Control.Applicative
22
+ import Control.Concurrent (threadDelay )
22
23
import Control.Concurrent.MVar.Lifted (MVar ,modifyMVar_ ,newMVar )
23
24
import Control.Exception.Lifted
24
25
import Control.Monad
@@ -330,10 +331,14 @@ runContainerAndExit getCmdArgs
330
331
before
331
332
#ifndef WINDOWS
332
333
runInBase <- liftBaseWith $ \ run -> return (void . run)
333
- oldHandlers <- forM ([sigINT | not keepStdinOpen] ++ [sigTERM]) $ \ sig -> do
334
- let sigHandler = do
335
- runInBase (readProcessNull Nothing envOverride " docker"
336
- [" kill" ," --signal=" ++ show sig,containerID])
334
+ oldHandlers <- forM ([sigINT,sigABRT,sigHUP,sigPIPE,sigTERM,sigUSR1,sigUSR2]) $ \ sig -> do
335
+ let sigHandler = runInBase $ do
336
+ readProcessNull Nothing envOverride " docker"
337
+ [" kill" ," --signal=" ++ show sig,containerID]
338
+ when (sig `elem` [sigTERM,sigABRT]) $ do
339
+ -- Give the container 30 seconds to exit gracefully, then send a sigKILL to force it
340
+ liftIO $ threadDelay 30000000
341
+ readProcessNull Nothing envOverride " docker" [" kill" ,containerID]
337
342
oldHandler <- liftIO $ installHandler sig (Catch sigHandler) Nothing
338
343
return (sig, oldHandler)
339
344
#endif
@@ -344,16 +349,19 @@ runContainerAndExit getCmdArgs
344
349
,[" -a" | not (dockerDetach docker)]
345
350
,[" -i" | keepStdinOpen]
346
351
,[containerID]])
347
- e <- try (callProcess'
348
- (if keepStdinOpen then id else (\ cp -> cp { delegate_ctlc = False }))
349
- cmd
350
- )
352
+ e <- finally
353
+ (try $ callProcess'
354
+ (\ cp -> cp { delegate_ctlc = False })
355
+ cmd)
356
+ (do unless (dockerPersist docker || dockerDetach docker) $
357
+ catch
358
+ (readProcessNull Nothing envOverride " docker" [" rm" ," -f" ,containerID])
359
+ (\ (_:: ReadProcessException ) -> return () )
351
360
#ifndef WINDOWS
352
- forM_ oldHandlers $ \ (sig,oldHandler) ->
353
- liftIO $ installHandler sig oldHandler Nothing
361
+ forM_ oldHandlers $ \ (sig,oldHandler) ->
362
+ liftIO $ installHandler sig oldHandler Nothing
354
363
#endif
355
- unless (dockerPersist docker || dockerDetach docker)
356
- (readProcessNull Nothing envOverride " docker" [" rm" ," -f" ,containerID])
364
+ )
357
365
case e of
358
366
Left (ProcessExitedUnsuccessfully _ ec) -> liftIO (exitWith ec)
359
367
Right () -> do after
0 commit comments