
`hGetContents` closes automatically the handle when the end of the file is reached. But in case of actions with exceptions, the end of the file could be not reached and the handle can be left temporally open, before
#12750: hGetContents leads to late/silent failures -------------------------------------+------------------------------------- Reporter: massimo.zaniboni | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Core Libraries | Version: 8.0.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: Incorrect result | Test Case: at runtime | https://github.com/massimo- | zaniboni/ghc_lazy_file_content_error Blocked By: | Blocking: Related Tickets: #9236 | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by rwbarton): the garbage collector reclaim it. That's right. It's normally no big deal to leave a file handle open for a little while. If this is a problem for you, then don't use `hGetContents`.
So the suggested pattern is managing file handles inside `bracket` resource management functions. But this can introduce problems in case of lazy evaluations, because the code processing the file content can be called after the handle is closed.
Indeed, mixing `hGetContents` and `bracket` isn't recommended. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12750#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler