File remains locked after crash
Neil Mitchell
ndmitchell at gmail.com
Wed May 3 09:58:13 EDT 2006
Hi,
On Windows, using GHCi 6.4.2, its possible to lock files open:
Change to a directory and run, at the prompt, with a file called
test2.txt already created:
> writeFile "test1.txt" (error "fail")
or
> readFile "test2.txt" >> putStrLn (error "fail")
The file test.txt is created, for the first. In both cases, the error
"fail" is given.
Now its impossible to delete the two files using Windows explorer,
because they are held open by GHCi. The only way I can find for GHCi
to release the file is to close GHCi and restart it.
It would be much nicer if GHCi had closed all open handles by the time
it returned back to the prompt.
Ross Paterson suggested an improved writeFile, which would close the
file on error:
> import Control.Exception
> import System.IO
>
> writeFile' :: FilePath -> String -> IO ()
> writeFile' f txt = bracket (openFile f WriteMode) hClose
> (flip hPutStr txt)
Which might work better.
Thanks
Neil
More information about the Glasgow-haskell-bugs
mailing list