[Haskell-cafe] Opening the same file multiple times
simonmar at microsoft.com
Mon Dec 12 13:57:11 EST 2005
On 12 December 2005 17:53, Donn Cave wrote:
> Quoth Einar Karttunen <ekarttun at cs.helsinki.fi>:
>> *** Exception: z: openFile: resource busy (file is locked)
> Now that I have access to a platform where ghc builds, I can
> duplicate your results - and in case it helps, here's another
> work-around. Apparently this "feature" uses POSIX filesystem
> locks, because it appears that we can exploit the brain-damaged
> semantics of that system: when a process closes a file, all
> its locks are cleared - whether they were acquired through that
> file descriptor or another unrelated open. It may be the first
> time anyone has ever benefited from this!
> import IO
> main = do
> fc <- openFile "z" ReadMode -- sacrificial handle
> fr <- openFile "z" ReadMode
> hClose fc
> fa <- openFile "z" AppendMode
> hPutStr fa "append this line\n"
> hGetLine fr >>= print
> If you may need to open more read handles later, you can add
> another extra close after the append handle.
Thank you (I suppose :-) for exposing a bug in GHC's file locking. It
doesn't have anything to do with POSIX filesystem locks, FWIW, it's just
a bug, in some very old code.
More information about the Haskell-Cafe