The dofile does not unlock the file until GC

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

The dofile does not unlock the file until GC

Alexander Gladysh
Hi, all!

(WinXP SP2, Lua 5.1.1)

I'm writing small utility, that takes some number of lua files,
pregenerated by Excel script, and merges them into single file, doing
some postprocessing. This utility should delete input files before it
exits.

For example (untested illustrational code):

 -- Sample input file looks like:
 -- return { table_data };
 --

 local result = { };
 for i = 1,9 do
   merge_to_result(result, dofile("filename_" .. i .. ".lua")
 end

 -- ... (*)

 for i = 1,9 do
   assert(os.remove("filename_" .. i .. ".lua"))
 end

The call to os.remove() fails, reporting the file is locked by
someprocess. If I add call to collectgarbage("collect") on line (*),
os.remove() succeeds. Looks like dofile() keeps file open until GC.

It is somewhat inconvenient. Is it a bug or intentional feature?

Thanks in advance,
Alexander.

Reply | Threaded
Open this post in threaded view
|

Re: The dofile does not unlock the file until GC

Roberto Ierusalimschy
> (WinXP SP2, Lua 5.1.1)
> 
> [...]
> 
> The call to os.remove() fails, reporting the file is locked by
> someprocess. If I add call to collectgarbage("collect") on line (*),
> os.remove() succeeds. Looks like dofile() keeps file open until GC.
> 
> It is somewhat inconvenient. Is it a bug or intentional feature?

This is weird. dofile (which uses luaL_loadfile) always closes the file
after parsing it (unless the file is stdin). Moreover, I don't see how
a garbage collection would close the file. Unlike files created to
be used by Lua, e.g., by io.open, the file created by loadfile is only
a FILE*; it is not a userdata, and cannot have a finalizer.

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: The dofile does not unlock the file until GC

Luiz Henrique de Figueiredo
In reply to this post by Alexander Gladysh
> The call to os.remove() fails, reporting the file is locked by
> someprocess. If I add call to collectgarbage("collect") on line (*),
> os.remove() succeeds. Looks like dofile() keeps file open until GC.

This is very strange. dofile never creates any Lua object for the file.
Also, luaL_loadfile take pains to always close the file.

Are you sure the files are not open by some other process?o

Could you please post the actual code that has the problem (ideally the
minimal example that exhibits the problem)?

--lhf

Reply | Threaded
Open this post in threaded view
|

Re: The dofile does not unlock the file until GC

Todor Totev
In reply to this post by Roberto Ierusalimschy
The call to os.remove() fails, reporting the file is locked by
someprocess. If I add call to collectgarbage("collect") on line (*),
os.remove() succeeds. Looks like dofile() keeps file open until GC.

This is weird. dofile (which uses luaL_loadfile) always closes the file
after parsing it (unless the file is stdin). Moreover, I don't see how
a garbage collection would close the file. Unlike files created to
be used by Lua, e.g., by io.open, the file created by loadfile is only
a FILE*; it is not a userdata, and cannot have a finalizer.

As you mentioned Excel, i'm supposing you are using Windows.
If this is the case, you can use Process Explorer utility from
http://www.sysinternals.com/Utilities/ProcessExplorer.html
to track down who keeps the file opened.
It might be explorer.exe (via some shell extension) or
your anti-virus/spyware program and the call to GC just gives more time
to it to finsih its job.
After installing the tool, use Find->Find Handle or Dll menu and
type some part of the file name of lua script. You may click the processes'
names in the list and will be guided to the process owning the handle.
Hope this will help you,
Todor


Reply | Threaded
Open this post in threaded view
|

Re: The dofile does not unlock the file until GC

Alexander Gladysh
In reply to this post by Alexander Gladysh
Hi, all!

Well, it looks like it was actually Excel locking those files. The
strange thing is that call to collectgarbage("collect") stable
triggered delay long enough for OS to unlock these files...

Anyway, sorry for extra noise.

Thanks,
Alexander.

On 8/3/06, Alexander Gladysh <[hidden email]> wrote:
Hi, all!

(WinXP SP2, Lua 5.1.1)

I'm writing small utility, that takes some number of lua files,
pregenerated by Excel script, and merges them into single file, doing
some postprocessing. This utility should delete input files before it
exits.