[Haskell-cafe] Problem on exception and laziness

Brandon Allbery allbery.b at gmail.com
Tue Apr 23 06:06:30 UTC 2019


At a guess, include the parseStat in the wrap2Maybe so that the input is
evaluated. Alternately, force the spine of the list produced by readFile so
that the entire file is read immediately.

Note that the error is from getContents, which produces a lazy list that
reads "in the background" as the resulting list is consumed (see
unsafeInterleaveIO): "files" in /proc are generated as they are read, so if
a process exits while you have an open handle on one of its files in /proc,
it will be invalidated on you like that.

On Tue, Apr 23, 2019 at 2:01 AM Magicloud Magiclouds <
magicloud.magiclouds at gmail.com> wrote:

> Hi,
> I am aware that laziness sometimes may cause exception handling in
> unexpected ways. Recently I have such a problem that I cannot figure
> out how to fix (simply).
>
> I have a function `watch` that parse the content of /proc/. And I use
> `catch` to wrap `readFile` call, since the data in /proc/ could be
> gone any time.
>
> ```Haskell
> wrap2Maybe :: IO a -> IO (Maybe a)
> wrap2Maybe f = catch ((<$>) Just $! f) (\(_ :: IOException) -> return
> Nothing)
>
> watch args = do
>   mStat <- wrap2Maybe (readFile ("/proc/" </> fp </> "stat")) >>=
>     return . flip (>>=) parseStat
>   print mStat
>   watch args
> ```
>
> But occasionally, my tool exits with message:
> GoM: /proc/1484/stat: hGetContents: does not exist (No such process)
>
> Since above code is the only `readFile` call which leads to
> hGetContents, I could not figure out what else should I do to catch
> the error.
> --
> 竹密岂妨流水过
> 山高哪阻野云飞
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.



-- 
brandon s allbery kf8nh
allbery.b at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20190423/a8e941bb/attachment.html>


More information about the Haskell-Cafe mailing list