lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On 18 July 2018 at 15:55, Roberto Ierusalimschy <> wrote:
>>   A few months ago I wrote some code to recursively dive into directories,
>> using something like:
>>       [...]
>>   I was exhausting file descriptors before my program finished [1].  Upon
>> investigation, it was because iterating over a directory uses a file
>> descriptor, and because GC had yet to kick in, I had a bazillion open
>> directories.  I had to change the iterator to close and release the
>> directory upon finishing instead of relying upon the GC.  And this is in
>> what I consider library code.
> I think a few rules could greatly improve things. Unfortunately, I was
> not following them :-)
> - As Tomas pointed out, iterators should release their resources as soon
> as they reach the last element. Sadly, my implementation of a directory
> iterator in PiL does not do that; but the implementation of io.lines in
> the standard library does :-)

The implementation of lfs.dir in LuaFileSystem does close the
directory handle as it reaches the last element. Unfortunately, that's
not good enough, because of course, when given a "for" iterator, users
can (and do) use "break".

-- Hisham