2 -----------------------------------------------------------------------------
4 -- Module : GHC.TopHandler
5 -- Copyright : (c) The University of Glasgow, 2001-2002
6 -- License : see libraries/base/LICENSE
8 -- Maintainer : cvs-ghc@haskell.org
9 -- Stability : internal
10 -- Portability : non-portable (GHC Extensions)
12 -- Support for catching exceptions raised during top-level computations
13 -- (e.g. @Main.main@, 'Control.Concurrent.forkIO', and foreign exports)
15 -----------------------------------------------------------------------------
18 module GHC.TopHandler (
19 runMainIO, runIO, runIOFastExit, runNonIO, reportStackOverflow, reportError
25 import Control.Exception
27 import Foreign.C ( CInt )
30 import GHC.Prim (unsafeCoerce#)
32 -- | 'runMainIO' is wrapped around 'Main.main' (or whatever main is
33 -- called in the program). It catches otherwise uncaught exceptions,
34 -- and also flushes stdout\/stderr before exiting.
35 runMainIO :: IO a -> IO a
36 runMainIO main = (do a <- main; cleanUp; return a) `catchException` topHandler
38 -- | 'runIO' is wrapped around every @foreign export@ and @foreign
39 -- import \"wrapper\"@ to mop up any uncaught exceptions. Thus, the
40 -- result of running 'System.Exit.exitWith' in a foreign-exported
41 -- function is the same as in the main thread: it terminates the
45 runIO main = catchException main topHandler
47 -- | Like 'runIO', but in the event of an exception that causes an exit,
48 -- we don't shut down the system cleanly, we just exit. This is
49 -- useful in some cases, because the safe exit version will give other
50 -- threads a chance to clean up first, which might shut down the
51 -- system in a different way. For example, try
53 -- main = forkIO (runIO (exitWith (ExitFailure 1))) >> threadDelay 10000
55 -- This will sometimes exit with "interrupted" and code 0, because the
56 -- main thread is given a chance to shut down when the child thread calls
57 -- safeExit. There is a race to shut down between the main and child threads.
59 runIOFastExit :: IO a -> IO a
60 runIOFastExit main = catchException main topHandlerFastExit
61 -- NB. this is used by the testsuite driver
63 -- | The same as 'runIO', but for non-IO computations. Used for
64 -- wrapping @foreign export@ and @foreign import \"wrapper\"@ when these
65 -- are used to export Haskell functions with non-IO types.
68 runNonIO a = catchException (a `seq` return a) topHandler
70 topHandler :: Exception -> IO a
71 topHandler err = catchException (real_handler err) topHandler
73 -- Make sure we handle errors while reporting the error!
74 -- (e.g. evaluating the string passed to 'error' might generate
75 -- another error, etc.)
77 real_handler :: Exception -> IO a
81 AsyncException StackOverflow -> do
85 -- only the main thread gets ExitException exceptions
86 ExitException ExitSuccess -> safeExit 0
87 ExitException (ExitFailure n) -> safeExit n
94 reportStackOverflow :: IO a
95 reportStackOverflow = do callStackOverflowHook; return undefined
97 reportError :: Exception -> IO a
99 handler <- getUncaughtExceptionHandler
103 -- SUP: Are the hooks allowed to re-enter Haskell land? If so, remove
105 foreign import ccall unsafe "stackOverflow"
106 callStackOverflowHook :: IO ()
108 -- try to flush stdout/stderr, but don't worry if we fail
109 -- (these handles might have errors, and we don't want to go into
110 -- an infinite loop).
113 hFlush stdout `catchException` \_ -> return ()
114 hFlush stderr `catchException` \_ -> return ()
116 cleanUpAndExit :: Int -> IO a
117 cleanUpAndExit r = do cleanUp; safeExit r
119 -- we have to use unsafeCoerce# to get the 'IO a' result type, since the
120 -- compiler doesn't let us declare that as the result type of a foreign export.
121 safeExit :: Int -> IO a
122 safeExit r = unsafeCoerce# (shutdownHaskellAndExit r)
124 -- NOTE: shutdownHaskellAndExit must be called "safe", because it *can*
125 -- re-enter Haskell land through finalizers.
126 foreign import ccall "shutdownHaskellAndExit"
127 shutdownHaskellAndExit :: Int -> IO ()