Program lives on with dead main thread
|Reported by:||guest||Owned by:|
|Type of failure:||None/Unknown||Difficulty:||Unknown|
|Test Case:||Blocked By:|
In a standalone GHC program, only the main thread is required to terminate in order for the process to terminate. Thus all other forked threads will simply terminate at the same time as the main thread (the terminology for this kind of behaviour is "daemonic threads").
This isn't always true in ghc, as the following program demonstrates:
% cat try.hs import Control.Concurrent import Control.Concurrent.STM import Control.Monad import System.Exit main :: IO () main = do x <- newTVarIO 0 forkIO $ forever $ do --atomically $ writeTVar x 42 --yield return () putStrLn "about to exit" exitSuccess putStrLn "dead"
% ghc -fforce-recomp try.hs [1 of 1] Compiling Main ( try.hs, try.o ) Linking try ... % ./try about to exit ^C^C
Instead of actually exiting, the program consumes 100% CPU despite the main thread being gone.
The behavior is slightly different with -threaded:
% ghc -fforce-recomp -threaded try.hs [1 of 1] Compiling Main ( try.hs, try.o ) Linking try ... % ./try ^C^C
Here it doesn't even execute the main thread.
% ./try +RTS -N2 try: Using large values for -N is not allowed by default. Link with -rtsopts to allow full control.
I don't understand what's up with this. 2 isn't what I'd call "large".
% ghc -fforce-recomp -threaded -rtsopts=all try.hs [1 of 1] Compiling Main ( try.hs, try.o ) Linking try ... % ./try +RTS -N2 about to exit
This version works as expected.
Anyway, I'm not sure whether this is a bug in the RTS or in the documentation, but they clearly don't agree.