[Haskell-cafe] System.Exit

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Thu Jul 5 00:20:40 EDT 2007


On Jul 5, 2007, at 0:04 , Thomas Conway wrote:

> 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

If you're making foreign calls and using forkIO, then there will be  
at least two OS threads running.  In this case, the process generally  
won't exit until all the OS threads do.  Since this is the OS's  
doing, as far as the Haskell runtime is concerned System.Exit has  
done the right thing, but the other OS thread(s) won't have been  
notified to shut down so they'll just keep going until told otherwise.

I think the proper action here is that your quitHandler sets an MVar/ 
TVar to indicate that things should start shutting down, then either  
shut down its thread or, if it's in the main thread, wait for the  
other threads to terminate and then invoke System.Exit.

-- 
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH




More information about the Haskell-Cafe mailing list