[Haskell-cafe] Why does sleep not work?

Manlio Perillo manlio_perillo at libero.it
Tue Feb 10 19:50:05 EST 2009

George Pollard ha scritto:
> [...]
>> So, it seems nanosleep get interruped by a signal.
> This works:
>> import System.Posix
>> main = do
>> 	putStrLn "Waiting for 5 seconds."
>> 	blockSignals $ addSignal sigVTALRM emptySignalSet
>> 	sleep 5
>> 	putStrLn "Done."
> So (see my earlier email) `sleep` is lying about what interrupts it :)
> - George

A possibly better solution is:

sleep' :: Int -> IO Int
sleep' n = do
   n' <- sleep n
   if n' == 0 then return 0 else sleep' n'

 From the trace, I see that nanosleep is being called 17 times here.

Another solution is to set RTS flag:
./bug_sleep +RTS -V0 -RTS

What strange is that the timer is created in non threaded RTS, too, but 
sleep is interrupted only with the threaded RTS.

This may be caused by an "incorrect" execution of a foreign function 
marked safe.

Regards  Manlio

More information about the Haskell-Cafe mailing list