<div dir="ltr">It's /proc that is messing you up here, I suspect: it's regenerated on every read system call, not on open, so if it's more than one block then it can go away between reading the blocks because there's nothing to generate the next block from. Flip side, because of this most files in /proc are kept small, so it's safe to read them all at once. (Memory images being an exception, but those are handled differently anyway.)<div><br></div><div>Moral of the story is you should probably use strict I/O with stuff under /proc, not (h)getContents or anything based on it like readFile.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Apr 23, 2019 at 2:50 AM Magicloud Magiclouds <<a href="mailto:magicloud.magiclouds@gmail.com">magicloud.magiclouds@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Thanks. I thought $! could help on this, so tried to avoid DeepSeq. Let me try.<br>
<br>
On Tue, Apr 23, 2019 at 2:06 PM Brandon Allbery <<a href="mailto:allbery.b@gmail.com" target="_blank">allbery.b@gmail.com</a>> wrote:<br>
><br>
> 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.<br>
><br>
> 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.<br>
><br>
> On Tue, Apr 23, 2019 at 2:01 AM Magicloud Magiclouds <<a href="mailto:magicloud.magiclouds@gmail.com" target="_blank">magicloud.magiclouds@gmail.com</a>> wrote:<br>
>><br>
>> Hi,<br>
>> I am aware that laziness sometimes may cause exception handling in<br>
>> unexpected ways. Recently I have such a problem that I cannot figure<br>
>> out how to fix (simply).<br>
>><br>
>> I have a function `watch` that parse the content of /proc/. And I use<br>
>> `catch` to wrap `readFile` call, since the data in /proc/ could be<br>
>> gone any time.<br>
>><br>
>> ```Haskell<br>
>> wrap2Maybe :: IO a -> IO (Maybe a)<br>
>> wrap2Maybe f = catch ((<$>) Just $! f) (\(_ :: IOException) -> return Nothing)<br>
>><br>
>> watch args = do<br>
>>   mStat <- wrap2Maybe (readFile ("/proc/" </> fp </> "stat")) >>=<br>
>>     return . flip (>>=) parseStat<br>
>>   print mStat<br>
>>   watch args<br>
>> ```<br>
>><br>
>> But occasionally, my tool exits with message:<br>
>> GoM: /proc/1484/stat: hGetContents: does not exist (No such process)<br>
>><br>
>> Since above code is the only `readFile` call which leads to<br>
>> hGetContents, I could not figure out what else should I do to catch<br>
>> the error.<br>
>> --<br>
>> 竹密岂妨流水过<br>
>> 山高哪阻野云飞<br>
>> _______________________________________________<br>
>> Haskell-Cafe mailing list<br>
>> To (un)subscribe, modify options or view archives go to:<br>
>> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
>> Only members subscribed via the mailman list are allowed to post.<br>
><br>
><br>
><br>
> --<br>
> brandon s allbery kf8nh<br>
> <a href="mailto:allbery.b@gmail.com" target="_blank">allbery.b@gmail.com</a><br>
<br>
<br>
<br>
-- <br>
竹密岂妨流水过<br>
山高哪阻野云飞<br>
<br>
And for G+, please use magiclouds#<a href="http://gmail.com" rel="noreferrer" target="_blank">gmail.com</a>.<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>brandon s allbery kf8nh</div><div><a href="mailto:allbery.b@gmail.com" target="_blank">allbery.b@gmail.com</a></div></div></div></div></div>