Correctly shutdown on SIGTERM/SIGINT (fixing bad exception handling code).

This commit is contained in:
Benjamin Summers 2020-05-01 15:32:08 -07:00
parent 80d900d542
commit e56e538ff3

View File

@ -564,9 +564,10 @@ main = do
hSetBuffering stdout NoBuffering hSetBuffering stdout NoBuffering
let onTermSig = throwTo mainTid UserInterrupt let onKillSig = throwTo mainTid UserInterrupt
Sys.installHandler Sys.sigTERM (Sys.Catch onTermSig) Nothing Sys.installHandler Sys.sigTERM (Sys.Catch onKillSig) Nothing
Sys.installHandler Sys.sigINT (Sys.Catch onKillSig) Nothing
CLI.parseArgs >>= \case CLI.parseArgs >>= \case
CLI.CmdRun ships -> runShips ships CLI.CmdRun ships -> runShips ships
@ -626,23 +627,20 @@ runShips = \case
async (runShipRestarting waitForKillRequ r o) async (runShipRestarting waitForKillRequ r o)
{- {-
This `waitAny` call never returns, so this runs until the main Since `spin` never returns, this will run until the main
thread is killed with an async exception. The one we expect is thread is killed with an async exception. The one we expect is
`UserInterrupt` which will be raised on this thread upon SIGKILL `UserInterrupt` which will be raised on this thread upon SIGKILL
or SIGTERM. or SIGTERM.
-}
res <- try (waitAny shipThreads)
{- Once that happens, we write to `killSignal` which will cause
Send the kill signal to all of the ships, and then wait for all all ships to be shut down, and then we `wait` for them to finish
of them to exit. before returning.
-} -}
let die = do atomically (putTMVar killSignal ()) let spin = forever (threadDelay maxBound)
for_ shipThreads waitCatch finally spin $ do
putStrLn "KING IS GOING DOWN"
case res of atomically (putTMVar killSignal ())
Left UserInterrupt -> die for_ shipThreads waitCatch
_ -> die
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------