From a628822d1419740ea7d084875a4364dbe9c17481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Thu, 24 Oct 2013 18:45:01 +0200 Subject: [PATCH] changed garbage collection of file handles a bit. although it was working, in hindsight i'm really not quite sure *how*, or rather: Lua seems to be smarter than I already gave it credit for. it apparently realized that a new reference to the object to be gc'ed was created in its finalizer, so it let it live. however, i'm not quite sure why that didn't lead to a pseudo-infinite loop. now we just keep the stream as an upvalue for the timer callback that actually closes the stream; also, removed the checks if event.timer exists. if people remove that it's really their own problem --- assets/opencomputers/lua/rom/lib/io.lua | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/assets/opencomputers/lua/rom/lib/io.lua b/assets/opencomputers/lua/rom/lib/io.lua index 4521e3b71..61d8e5f22 100644 --- a/assets/opencomputers/lua/rom/lib/io.lua +++ b/assets/opencomputers/lua/rom/lib/io.lua @@ -267,11 +267,10 @@ function file.new(mode, stream, nogc) metatable.__gc = function(self) -- file.close does a syscall, which yields, and that's not possible in -- the __gc metamethod. So we start a timer to do the yield/cleanup. - if type(event) == "table" and type(event.timer) == "function" then - event.timer(0, function() - self:close() - end) - end + local stream = self.stream -- only keep the stream as an upvalue + event.timer(0, function() + stream:close() + end) end end return setmetatable(result, metatable)