File tree 3 files changed +53
-0
lines changed 3 files changed +53
-0
lines changed Original file line number Diff line number Diff line change @@ -165,6 +165,7 @@ library
165
165
Distribution.Client.Security.HTTP
166
166
Distribution.Client.Setup
167
167
Distribution.Client.SetupWrapper
168
+ Distribution.Client.Signal
168
169
Distribution.Client.SolverInstallPlan
169
170
Distribution.Client.SourceFiles
170
171
Distribution.Client.SrcDist
Original file line number Diff line number Diff line change @@ -113,6 +113,8 @@ import Distribution.Client.Manpage (manpageCmd)
113
113
import Distribution.Client.ManpageFlags (ManpageFlags (.. ))
114
114
import Distribution.Client.Utils
115
115
( determineNumJobs , relaxEncodingErrors )
116
+ import Distribution.Client.Signal
117
+ ( installTerminationHandler )
116
118
import Distribution.Client.Version
117
119
( cabalInstallVersion )
118
120
@@ -170,6 +172,7 @@ import Control.Exception (try)
170
172
--
171
173
main :: IO ()
172
174
main = do
175
+ installTerminationHandler
173
176
-- Enable line buffering so that we can get fast feedback even when piped.
174
177
-- This is especially important for CI and build systems.
175
178
hSetBuffering stdout LineBuffering
Original file line number Diff line number Diff line change
1
+ {-# LANGUAGE CPP #-}
2
+ module Distribution.Client.Signal
3
+ ( installTerminationHandler
4
+ , Terminated (.. )
5
+ )
6
+ where
7
+
8
+ import qualified Control.Exception as Exception
9
+
10
+ #ifndef mingw32_HOST_OS
11
+ import Control.Concurrent (myThreadId )
12
+ import Control.Monad (void )
13
+ import qualified System.Posix.Signals as Signals
14
+ #endif
15
+
16
+ -- | Terminated is an asynchronous exception, thrown when
17
+ -- SIGTERM is received. It's to 'kill' what 'UserInterrupt'
18
+ -- is to Ctrl-C.
19
+ data Terminated = Terminated
20
+
21
+ instance Exception. Exception Terminated where
22
+ toException = Exception. asyncExceptionToException
23
+ fromException = Exception. asyncExceptionFromException
24
+
25
+ instance Show Terminated where
26
+ show Terminated = " terminated"
27
+
28
+ -- | Install a signal handler that initiates a controlled shutdown on receiving
29
+ -- SIGTERM by throwing an asynchronous exception at the main thread. Must be
30
+ -- called from the main thread.
31
+ --
32
+ -- It is a noop on Windows.
33
+ --
34
+ installTerminationHandler :: IO ()
35
+
36
+ #ifdef mingw32_HOST_OS
37
+
38
+ installTerminationHandler = return ()
39
+
40
+ #else
41
+
42
+ installTerminationHandler = do
43
+ mainThreadId <- myThreadId
44
+ void $ Signals. installHandler
45
+ Signals. sigTERM
46
+ (Signals. CatchOnce $ Exception. throwTo mainThreadId Terminated )
47
+ Nothing
48
+
49
+ #endif
You can’t perform that action at this time.
0 commit comments