FFI and Control-C

Simon Marlow simonmar@microsoft.com
Wed, 1 Aug 2001 10:04:48 +0100

> So, my questions are:
>   1) Does anyone know what exactly happens when control-C is
>      pressed during foreign function evaluation?

Yes.  Two cases: if the program hasn't requested that Ctrl-C be trapped
(using the Posix library's installHandler), then the SIGINT handler just
sets a flag which causes the scheduler to kill all the running threads
and exit.  If the program is in a foreign call, then the scheduler won't
get to run until the foreign call returns.

If the program has installed a handler for SIGINT (which GHCi does),
then the SIGINT handler in the RTS sets a different flag.  The next time
the scheduler runs, it creates a new thread for the handler and puts it
on the run queue.  In the case of GHCi, the handler thread raises an
exception in the main thread, which is caught by the GHCi loop.  Again,
if the main thread is in a foreign call, none of this will happen until
the call returns.

>   2) Does anyone know how this can be controlled in a more
>      finegrained way?

We don't have a good solution for this at the moment.  Where possible,
don't do blocking foreign calls.  The threadWaitRead/threadWaitWrite
primitives can be used to avoid blocking if you know the file descriptor
that the foreign call is going to block on.