schedule: re-entered unsafely - with heavy concurrent load

Einar Karttunen ekarttun at
Fri Feb 18 17:15:04 EST 2005


I am having problems with the threaded rts (6.5.20050207)
dying with 'schedule: re-entered unsafely'. The same code 
seems to work with 6.2.2 with threaded rts.

The exact error message is:
"foo: schedule: re-entered unsafely.
   Perhaps a 'foreign import unsafe' should be 'safe'?"

I originally notices the problem with networking code which 
didn't use any of Network (nor threadWaitX). However the error 
appeared even with the standard libraries tipping in the direction 
of a bug in the rts.

The code works most of the time, but has a small probability of 
failing when load is high. The test case implements a dummy web 
server which can be tried with e.g. apache benchmark (ab),
/usr/sbin/ab2 -c 1000 -n 5000
usually crashes the code when run a few times.
The code should be compiled and run as 
ghc --make -threaded foo.hs -o foo && ./foo 8080

import Network
import Control.Concurrent
import qualified Control.Exception as E
import System
import System.IO

main = do [port] <- getArgs
          server port

server port = do
  s <- listenOn $ PortNumber $ fromIntegral $ read port
  acceptLoop s

acceptLoop sock = do
  (csock,_,_) <- accept sock
  forkIO (handle csock `E.catch` print)
  acceptLoop sock

handle sock = do
  hPutStr sock "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n"
  hPutStr sock ("This is the body for the request to\n"++"hope you like this.\n\nnow garbage:\n")
  hClose sock

- Einar Karttunen

More information about the Glasgow-haskell-users mailing list