Catching Control-C exception in GHC (Haskell)?

The most reliable solution for me (at least on Linux), has been to install a signal handler using System.Posix.Signals. I was hoping for a solution that would not require this, but the real reason I posted the question was that I wanted to know why GHC behaved the way it did. As explained on #haskell, a likely explanation is that GHC behaves this way so that the user can always Control-C an application if it hangs.

Still, it would be nice if GHC provided a way to affect this behavior without the somewhat lower-level method that we resorted to :).

Disclaimer: I'm not familiar with GHC internals and my answer is based on grepping the source code, reading the comments, and making guesses. The main function you define is in fact wrapped by runMainIO defined in GHC. TopHandler (this is further confirmed by looking at TcRnDriver.

Lhs): -- | 'runMainIO' is wrapped around 'Main. Main' (or whatever main is -- called in the program). It catches otherwise uncaught exceptions, -- and also flushes stdout\/stderr before exiting.

RunMainIO :: IO a -> IO a runMainIO main = do main_thread_id return () Just tid -> throwTo tid (toException UserInterrupt) a IO () #ifdef mingw32_HOST_OS install_interrupt_handler handler = do _ case event of ControlC -> handler Break -> handler Close -> handler _ -> return () return () #else #include "rts/Signals. H" -- specialised version of System.Posix.Signals. InstallHandler, which -- isn't available here.

Install_interrupt_handler handler = do let sig = CONST_SIGINT :: CInt _ On Windows, things are done differently, which probably explains ja's observation.

I tried it on Ubuntu 9.10, which has libedit, and it behaved as your seeing.It worked for me on Windows Vista! This Trac ticket may help assess get current sitch for ghc: hackage.haskell.org/trac/ghc/ticket/2301.

Readline and libedit are beside the point. Regardless of what you use, you still have to deal with this problem. I'd prefer that this work on Linux and Windows, but I'll settle for Linux :).

– pheaver Mar 1 '10 at 11:13.

Wei Hu is correct; the Haskell runtime system deliberately aborts the program when a second control-C is pressed. To get the behavior one might expect: import Control. Exception as E import Control.

Concurrent import System.Posix. Signals main = do tid.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions