Shutting Down the RTS

Wolfgang Thaller
Mon, 13 Jan 2003 00:41:17 +0100

I'm almost ready to send in a patch that should fix most of the current 
issues with the threaded RTS.
But.... I'm stuck at the problem of terminating the RTS in a proper way.

According to the GHC manual, a concurrent Haskell program should 
terminate when the main action terminates. This sounds reasonable and 
matches the behaviour of C programs on most platforms.

In the threaded RTS, this behaviour has never been implemented. We 
can't simply return from schedule(), because we might no longer be 
running in the thread of the RTS main() routine. The thread where 
rts_mainEvalIO was called might be busy executing some foreign code 
that we know nothing about.

(Im)possible solution #1: As soon as the main action terminates, call 
shutdownHaskellAndExit(). At first, this seems to work fine.
But then, shutdownHaskellAndExit() comes along and tries to run 
finalizers. For every finalizer, the RTS is started back up again using 
rts_mainEvalIO(), and this time, we really have to exit by returning 
from rts_mainEvalIO().

Possible solution #2: Forget about running finalizers at program 
termination and just exit().
In most of the situations where I'd use finalizers, I don't need to run 
them upon program termination, the OS cleans up after me. Also, the 
finalizers are not run in the correct order anyway, and there are 
situations where running them in the wrong order might be worse than 
not running them at all.
And, most of all, solution #2 is easy to implement.

Can somebody suggest a third solution, or shall I go for #2 for the 
time being?