[Haskell-cafe] MissingH bracketCD (aka bracketCWD) bug -- this is
the infamous lazy io, right?
nicolas.pouillard at gmail.com
Mon Mar 23 05:45:29 EDT 2009
Excerpts from Thomas Hartman's message of Mon Mar 23 09:08:41 +0100 2009:
> I got bitten by a bug (well, I call it bug) in bracketCD from
> HSH/MissingH demonstrated by the following code
> bracketCD is very useful for sysadminny one-offs, I use it all the
> time, but..... I suspect that unless people are very careful, this
> behavior will affect other users of bracketCD, in potentially very
> subtle and tricky ways.
> 1) -- is there a more elegant way to deal with lazy io than the hack
> below? (putStrLn the last character)
Yes without changing the bracketCD function you can use a strict 'return'
function to help you avoid leaks.
return' :: (Monad m, NFData sa) -> sa -> m sa
return' x = rnf x `seq` return x
> 2) -- should MissingH function bracketCD be fixed, and if so how?
Not completely while staying in the full 'IO' monad.
Have a look at the strict-io package  that goes in this direction.
myBracketCWD :: (NFData sa, Show sa) => FilePath -> SIO sa -> IO sa
myBracketCWD fp action = do
oldcwd <- getCurrentDirectory
a <- SIO.run action
rnf a `seq` setCurrentDirectory oldcwd
The same function could be more deeply in the 'SIO' monad by returning
in the 'SIO' monad. However this would require to first wrap
getCurrentDirectory and setCurrentDirectory in the 'SIO' monad.
More information about the Haskell-Cafe