[Haskell-cafe] how to catch keyboard interrupts?

Uwe Hollerbach uhollerbach at gmail.com
Sat Feb 23 15:35:35 EST 2008

Hi, all, I am continuing to mess with my little scheme interpreter,
and I decided that it would be nice to be able to hit control-C in the
middle of a long-running scheme computation to interrupt that and
return to the lisp> prompt; hitting control-C and getting back to the
shell prompt works, but is a little drastic. So I looked at
System.Posix.Signals and after a bit of messing about got the

> mysighandler =
>   Catch (do hPutStrLn stderr "caught a signal!"
>             fail "Interrupt!")
> runREPL :: IO ()
> runREPL =
>   do getProgName >>= writeHdr
>      env <- setupBindings [] True
>      runInit env
>      installHandler sigINT mysighandler Nothing
>      installHandler sigQUIT mysighandler Nothing
>      doREPL env

This compiles just fine, the interpreter runs as usual... but the
added code doesn't seem to do anything. You can probably guess
already... the print statement in mysighandler is there to see if it
actually caught a signal. It does: I see "caught a signal!" just fine,
in fact I see dozens of them as I lean on the control-C; but now my
scheme calculation doesn't get interrupted at all! I see in the
System.Posix.Signals documentation that the signal handler gets
invoked in a new thread; is this the source of the problem? If so,
what should I do to fix it? I'm afraid that sort of stuff is still
beyond my haskell-fu...

many thanks!


More information about the Haskell-Cafe mailing list