Re: File remains locked after crash

Ross Paterson <[email protected]> Mon, 1 May 2006 17:12:52 +0100
Newsgroups gmane.comp.lang.haskell.hugs.bugs
Message-ID <[email protected]>
On Mon, May 01, 2006 at 04:32:30PM +0100, Neil Mitchell wrote:
> On Windows, Hugs CVS Head:
> 
> Change to a directory and run, at the prompt:
> writeFile "test.txt" (error "fail")
> 
> The file test.txt is created, and the error "fail" is given.
> 
> Now flipping to Windows explorer, try to delete the file test.txt and
> you can't because its still held open by hugs. The only way I can find
> for Hugs to release the file is to close hugs and restart it.
> 
> My guess is that the handle that is openned by writeFile is not closed on 
> error.

This should be a better implementation of writeFile:

	import Control.Exception
	import System.IO

	writeFile' :: FilePath -> String -> IO ()
	writeFile' f txt = bracket (openFile f WriteMode) hClose
				(flip hPutStr txt)