Opened 2 years ago

Closed 2 years ago

#7675 closed bug (duplicate)

Program lives on with dead main thread

Reported by: guest Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.6.2
Keywords: Cc:
Operating System: Linux Architecture: x86
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Revisions:

Description

http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Concurrent.html#g:13 says:

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.

Change History (1)

comment:1 Changed 2 years ago by simonmar

  • difficulty set to Unknown
  • Resolution set to duplicate
  • Status changed from new to closed

The main bug is an instance of #367. You can make it work with 7.8.1 using -fno-omit-yields and compiling with -O to ensure that enough stuff gets inlined (otherwise you'd need to compile the libraries with -fno-omit-yields too).

A "large value of -N" is one that is greater than the number of cores in your machine.

Note: See TracTickets for help on using tickets.