[darcs-users] darcs patch: add suggested fix to readFile policy war... (and 1 more)

Petr Rockai me at mornfall.net
Thu Feb 12 20:14:52 UTC 2009


Eric Kow <kowey at darcs.net> writes:
> Whoops! Looks like you just crossed patches with Petr
Yeah. :|

> add suggested fix to readFile policy warning
> --------------------------------------------
>> Simon Michael <simon at joyful.com>**20090212183255
>>  Ignore-this: f8430012bbc4113a5e6b17c22409b59f
>> ] hunk ./tests/haskell_policy.sh 28
>> -        "readFile doesn't ensure the file is closed before it is deleted!" \
>> +        "Prelude.readFile doesn't ensure the file is closed before it is deleted!\nConsider import Data.ByteString.Char8 as B (readFile), B.readFile instead." \
>
> I'm not sure I buy this explanation.
> I do see this in the haskell policy file:
>
>  B # importing readFile from Data.ByteString as B, is allowed
>
> but it's not much of an explanation.  Why is this allowed when the
> Prelude.readFile is not?  I have a sneaking suspicion this exception
> was just thrown in to make haskell_policy shut up :-P
>
> Can somebody explain this to me?  Otherwise, to avoid people wrongly
> thinking that the solution to the readFile issue is just to use
> B.readFile, I'm not going to apply this patch.
Let me explain then. In return, I'd be grateful if you could file this
somewhere where people find it the next time this comes around.

The problem with Prelude readFile is that it's based on hGetContents, which is
lazy by definition. This also means that unless you force consumption of the
produced list, it will keep an fd open for the file, possibly
indefinitely. This is called a fd leak. Other than being annoying and if done
often, leading to fd exhaustion and failure to open any new files (which is
usually fatal), it also prevents the file to be unlinked (deleted) on win32.

On the other hand, *strict* bytestring version of readFile will read the whole
file into a contiguous buffer, *close the fd* and return. This is perfectly
safe with regards to fd leaks. Btw., this is *not* the case with lazy
bytestring variant of readFile, so that one is unsafe.

Yours,
   Petr.

-- 
Peter Rockai | me()mornfall!net | prockai()redhat!com
 http://blog.mornfall.net | http://web.mornfall.net

"In My Egotistical Opinion, most people's C programs should be
 indented six feet downward and covered with dirt."
     -- Blair P. Houghton on the subject of C program indentation


More information about the darcs-users mailing list