writeFile/readFile on multiple threads leads to error

Bulat Ziganshin bulat.ziganshin at gmail.com
Wed Apr 22 08:44:43 EDT 2009

Hello Neil,

Wednesday, April 22, 2009, 4:22:01 PM, you wrote:

you can try to use POSIX open/read/write/close calls

> Hi,

> I've got a multi-threaded application which occasionally generates
> failures in openFile. I haven't been able to reproduce the errors
> reliably, the code is way too large to send over, and any small
> attempts at invoking the same problem don't seem to work. Despite the
> uselessness of the bug report, I thought I'd share what I've seen and
> how I fixed it.

> I have many threads, which read and write files. Every so often one
> thread will write a file, then another thread will read the same file
> - but fail during the open call. There are locks to ensure that the
> write call finishes before the read call begins. I modified the code
> to give:

> do print ("READ START",x) ; res <- readFile x ; print ("READ STOP",x)
> ; return res

> do print ("WRITE START",x); writeFile x src ; print ("WRITE STOP",x)

> I then get on the console:

> openFile doesn't have permission to open foo.

> The writeFile/readFile are happening in different threads, and they
> usually succeed - but not always. The bug seems to go away when I add
> performGC just after writeFile. My guess is that something in the
> openFile/hClose pair isn't really closed until a garbage collection
> happens. All this is using GHC 6.10.2 on XP through Cygwin.

> I'm happy to supply more details if you can think of anything that will help,

> Thanks

> Neil
> _______________________________________________
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users at haskell.org
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users

Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com

More information about the Glasgow-haskell-users mailing list