Skip to content

Tight (non-allocating) loop freezes the scheduler

Here is a program that spawns a thread from the main thread, which tries to constantly write out a message to the console.

module Main where
 
import Control.Concurrent (forkIO)
 
main :: IO ()
main = do
  _ <- forkIO $ runForever $ putStrLn "Hey"
  runForever $ return ()
 
runForever :: IO () -> IO ()
runForever action = action >> runForever action

If you compile it with 'ghc main', it works correctly - it prints out the message continuously, and you can terminate it by pressing Ctrl-C. However, if you compile it with 'ghc -O main' (or -O2, or -O3...), it doesn't print out anything, and the only way to exit is to kill the process from Task Manager.

This was reproducable with GHC 7.10.1, on a Windows 7 x64 machine, with an AMD A4-5300 APU.

''EDIT: As it turns out, using "yield" instead of "return ()" solves the problem. It seems I misunderstood how forkIO works. However, I'm not sure if the current working is intentional or not, so I think I should leave this ticket open - just to be on the safe side.''

Edited by Simon Peyton Jones
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information