[Haskell-cafe] System.Exit

Thomas Conway drtomc at gmail.com
Thu Jul 5 00:04:32 EDT 2007


Hi All,

Can anyone tell me what System.Exit.exitWith is actually supposed to
do? As far as I can tell, it seems to be a synonym of (return ()).

Okay, I'll stop being provocative and try and be helpful.

So I have a web server, which like the one in The Literature(TM),
essentially has a main loop:

doit sok = do
    (reqSok,reqAddr) <- Network.Socket.accept sok
    forkIO (handleRequest reqSok reqAddr)
    doit sok

This is all well and good, but how do you *stop* a server? Well, you
have a request (blah blash auth blah blah) which tells it to shut
down, /quit for example.

I have a function to handle the quit request that looks something like:

quitHandler sok addr .... = do
    tidyUpEverything ....
    sendOkResponse sok
    sClose sok
    System.Exit.exitWith ExitSuccess

All nice and simple. All except one detail: it doesn't actually work.

It prints

exit: ExitSuccess

but the "doit" loop keeps going. Of course, it goes totally spacko,
because of the call to tidyUpEverything, but it doesn't exit.

So, if I set an IORef/TVar inside quitHandler which I inspect either
just before or just after the call to Network.Socket.accept, I could
exit the loop, but that only helps once the next request comes in.

I contemplated a solution involving Control.Exception.throwTo, but I
actually read the doco (!) which states the following:

<quote>
If the target thread is currently making a foreign call, then the
exception will not be raised (and hence throwTo will not return) until
the call has completed. This is the case regardless of whether the
call is inside a block or not.
</quote>

So no joy there.

Ideas anyone?

And is exitWith broken, or is it the doco that's broken?

cheers,
T.
-- 
Dr Thomas Conway
drtomc at gmail.com

Silence is the perfectest herald of joy:
I were but little happy, if I could say how much.


More information about the Haskell-Cafe mailing list