Simon Marlow wrote:
>
> [ replying to private mail, but cc'ing to the list ]
>
> George Russell asks if he can write a function that traps ^C, with type:
>
> > allowInterrupts :: IO a -> IO a -> IO a
>
> You can implement this as follows: the signal handler needs to send an
> exception to the original thread to tell it the signal was caught, and the
> original thread then runs the user handler.
[implementation and patch snipped]
This is a very nice function but unfortunately it still doesn't quite work, because
of the delay in executing the interrupt action. (I write having applied the
patch and tried the function out with a little program of my own.) I suppose it is
not so bad that
allowInterupts action handler
may delay the handler for a few moments. However what IS bad is that if the
action terminates before the interrupt gets processed, the whole program
is embarassingly terminated with a "Fail: interrupted". (This has just happened.)
I suppose it is possible to work around this particular problem by modifying
allowInterrupts to use an MVar to see if the action has terminated yet.
(Or is it? How do you avoid a race condition?) But this isn't exactly ideal.
So could there be an IO action which yields until all pending interrupt
handlers have finished?