RE: [Haskell-cafe] Spurious program crashes

On 21 November 2005 16:43, Joel Reymont wrote:
I'm being quite careful with resources these days. The outstanding issues are
1) Crashes on Mac OSX that are not reproduced on Linux, Windows, etc.
2) Some kind of a problem with Chan. getChanContents retrieves things smoothly, readChan only does it for the first few lines. Simon? Anyone?
After subsequent dicsussion, do you still think something strange was going on here? The code does look strange: logger :: MVar () -> IO () logger die = do empty <- isEmptyChan parent unless empty $ do x <- readChan parent putStrLn x alive <- isEmptyMVar die when (alive || not empty) $ logger die so this basically loops until there are no messages in the channel, and then exits. Is that what you wanted, or did you want it to keep reading from the channel until told to die? STM is a better solution, as already suggested. Without STM, the best way to do this is to multiplex everything into a single channel (i.e. send the die message down the channel).
3) Different performance of the logger thread on Mac OSX and Windows.
I'm having thousands of threads write their trace messages to a Chan. The logger On Windows I only see the first few lines of output when using isEmptyChan/readChan to retrieve values in a loop. On Mac OSX I do see smooth output.
Context switch behaviour might be different between MacOS X and Windows. With the above code, it might be that the logger thread found an empty channel at sp,e point and exited. Does that make sense?
On Windows I run out of memory because all the output sent to the chan builds up and is never processed. I can process it by replacing isEmptyChan/readChan with getChanContents but then my logger thread hangs forever (right semantics) and hangs everything else that waits for the logger thread to check an MVar and exit.
yes, because the logger thread has exited. Cheers, SImon

On Nov 23, 2005, at 1:18 PM, Simon Marlow wrote:
After subsequent dicsussion, do you still think something strange was going on here?
Yes, but in a different thread. The "Postmortem" one.
so this basically loops until there are no messages in the channel, and then exits. Is that what you wanted, or did you want it to keep reading from the channel until told to die?
I probably made a mistake someplace as I wanted to read until told to die but ONLY if the channel was empty. I replaced that code with Tomasz's elegant solution so now I read until Nothing is read from the channel. logger :: Handle -> IO () logger h = do ss <- getChanContents parent logger' ss where logger' [] = return () logger' (Nothing:_) = return () logger' ((Just x):xs) = do putStrLn x hPutStrLn h x logger' xs yield For whatever reason this generates empty lines with some sort of an unprintable character at the beginning. It prints these to the screen but not to the file. Of course it also prints what it's supposed to but the garbage shows both on Windows and the Mac.
STM is a better solution, as already suggested. Without STM, the best way to do this is to multiplex everything into a single channel (i.e. send the die message down the channel).
Right. Thanks, Joel -- http://wagerlabs.com/
participants (2)
-
Joel Reymont
-
Simon Marlow