[Haskell-cafe] MissingH bracketCD (aka bracketCWD) bug -- this is
the infamous lazy io, right?
Nicolas Pouillard
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 [1] that goes in this direction.
myBracketCWD :: (NFData sa, Show sa) => FilePath -> SIO sa -> IO sa
myBracketCWD fp action = do
oldcwd <- getCurrentDirectory
setCurrentDirectory fp
a <- SIO.run action
rnf a `seq` setCurrentDirectory oldcwd
return a
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.
Best regards,
[1]: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/strict-io
--
Nicolas Pouillard
More information about the Haskell-Cafe
mailing list