packages feed

nova-nix-0.11.0.0 / src / Nix / Signals.hs

{-# LANGUAGE CPP #-}-- | The signals nova-nix takes as an interrupt.---- Upstream takes SIGINT, SIGTERM and SIGHUP alike as an interrupt: each-- stops what runs, its builds with it, and the command ends saying it was-- interrupted.  The GHC runtime throws 'UserInterrupt' to the main thread-- at SIGINT, but lets SIGTERM and SIGHUP end the process at once, which-- would leave a builder, in a session of its own, running on.  Windows-- has neither signal.module Nix.Signals  ( interruptOnTermination,  )where#if !defined(mingw32_HOST_OS)import Control.Concurrent (mkWeakThreadId, myThreadId, throwTo)import Control.Exception (AsyncException (UserInterrupt))import Data.Foldable (for_, traverse_)import System.Mem.Weak (deRefWeak)import System.Posix.Signals (Handler (Catch), installHandler, sigHUP, sigTERM)#endif-- | Have SIGTERM and SIGHUP throw 'UserInterrupt' to the calling thread,-- as the GHC runtime throws it to the main thread at SIGINT, for the rest-- of the process's life.  Called from the main thread, which the handler-- holds weakly, as the runtime's own does, so that it may still be found-- deadlocked.interruptOnTermination :: IO ()#if defined(mingw32_HOST_OS)interruptOnTermination = pure ()#elseinterruptOnTermination = do  mainThread <- myThreadId >>= mkWeakThreadId  let interrupt = Catch (deRefWeak mainThread >>= traverse_ (`throwTo` UserInterrupt))  for_ [sigTERM, sigHUP] $ \signal -> installHandler signal interrupt Nothing#endif