[GHC] #9236: hGetContents leads to late/silent failures

GHC ghc-devs at haskell.org
Tue Jun 24 18:39:06 UTC 2014


#9236: hGetContents leads to late/silent failures
-------------------------------+-------------------------------------------
        Reporter:  dfeuer      |            Owner:
            Type:  feature     |           Status:  new
  request                      |        Milestone:
        Priority:  normal      |          Version:  7.8.2
       Component:              |         Keywords:
  libraries/base               |     Architecture:  Unknown/Multiple
      Resolution:              |       Difficulty:  Easy (less than 1 hour)
Operating System:              |       Blocked By:
  Unknown/Multiple             |  Related Tickets:
 Type of failure:  Other       |
       Test Case:              |
        Blocking:              |
-------------------------------+-------------------------------------------
Changes (by dfeuer):

 * failure:  None/Unknown => Other
 * cc: hvr, ekmett (added)
 * component:  Compiler => libraries/base
 * difficulty:  Unknown => Easy (less than 1 hour)
 * version:  7.6.3 => 7.8.2


Old description:

> A common newbie error:
> {{{
> #!haskell
> withFile "file" ReadMode hGetContents >>= putStr
> }}}
>
> The problem, of course, is that the result of `withFile "file" ReadMode
> hGetContents` isn't forced until `putStr` is executed, at which point the
> file has already been closed. The Haskell report doesn't specify what it
> will find, and, at least in 7.6.3, it finds the string empty. The
> behavior that seems most correct to me would be to guarantee that if a
> file has not been completely read (or read up to an I/O error) when it is
> closed, then its contents should be reported as something like `"Four
> score and seven years ag"++error "Forcing a suspended computation led to
> an attempted read from a handle that was already closed."` Since the
> current system apparently puts an `[]` as a temporary marker while
> waiting to see what the rest of the list will be, I imagine that the fix
> is as simple as putting the error thunk there instead.

New description:

 A common newbie error:
 {{{
 #!haskell
 withFile "file" ReadMode hGetContents >>= putStr
 }}}

 The problem, of course, is that the result of `withFile "file" ReadMode
 hGetContents` isn't forced until `putStr` is executed, at which point the
 file has already been closed. The Haskell report doesn't specify what it
 will find, and, at least in 7.6.3, it finds the string empty. The behavior
 that seems most correct to me would be to guarantee that if a file has not
 been completely read (or read up to an I/O error) when it is closed, then
 its contents should be reported as something like `"Four score and seven
 years ag"++error "Forcing a suspended computation led to an attempted read
 from a handle that was already closed."` My suggested fix is in a comment
 below.

--

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/9236#comment:2>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list