writeFile/readFile on multiple threads leads to error
Claus Reinke
claus.reinke at talk21.com
Wed Apr 22 09:27:07 EDT 2009
>>> do print ("READ START",x) ; res <- readFile x ; print ("READ STOP",x)
>>> ; return res
>>
>> Unless you've defined your own version of 'readFile', to mean read
>> entire file now, the first 'print' is optimistic and the second 'print' is a
>> lie.
>
> readFile calls openFile >>= hGetContents. It's the openFile that
> causes the problem, so READ START happens before openFile and READ
> STOP happens after openFile. The lazy semantics of the actual reading
> don't seem to have an effect.
Just want to make sure that it isn't the latent hClose from readFile.
Once upon a time, I spend a lot of time figuring out why nhc98 wouldn't
work on windows, until I noticed that its code used to rely on unixy file
system tricks like readFile followed by removeFile, followed by looking
at the contents.
main = do
writeFile "data" "file contents"
r <- readFile "data"
writeFile "data" "different contents"
print r
*Main> main
*** Exception: data: openFile: permission denied (Permission denied)
> I did try changing to the strict bytestring file read, and that gave
> exactly the same error - apart from openBinaryFile was crashing rather
> than openFile.
Weren't there versions of bytestring that didn't close the file early enough?
Just checking,
Claus
> Thanks
>
> Neil
>
>>> do print ("WRITE START",x); writeFile x src ; print ("WRITE STOP",x)
>>>
>>> I then get on the console:
>>>
>>> WRITE START foo
>>> WRITE STOP foo
>>> READ START foo
>>> 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
>>
> _______________________________________________
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users at haskell.org
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
More information about the Glasgow-haskell-users
mailing list