lunarmodules / luafilesystem

LuaFileSystem is a Lua library developed to complement the set of functions related to file systems offered by the standard Lua distribution.
https://lunarmodules.github.io/luafilesystem/
MIT License
900 stars 291 forks source link

dir was not closed if you break the iterator in Lua 5.3 #139

Closed xuzhen closed 4 years ago

xuzhen commented 4 years ago
Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
> require 'lfs'
table: 0x560a3d27abb0
> while true do; for dir in lfs.dir('/') do; break; end; end
stdin:1: cannot open /: Too many open files
stack traceback:
    [C]: in function 'lfs.dir'
    stdin:1: in main chunk
    [C]: in ?

related commit: https://github.com/keplerproject/luafilesystem/commit/842505b6a33d0b0e2445568ea42f2adbf3c4eb77

hishamhm commented 4 years ago

@xuzhen Yes, this is a limitation of Lua 5.3. This does not happen on Lua 5.4 because there is explicit support for closing resources in the for loop. In Lua 5.3, the workaround is to call lfs.dir in a separate line to get the dir_obj and close it yourself.