mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-13 01:10:19 -04:00
close handles on procs in reverse order
also explicitly close stdout in cat. proc closes it, but this is cleaner
This commit is contained in:
parent
b117cbf89b
commit
ad5443c415
@ -35,3 +35,5 @@ for i = 1, #args do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
io.stdout:close()
|
||||
|
@ -45,7 +45,7 @@ local cols =
|
||||
{"THREADS", function(_,p)
|
||||
-- threads are handles with mt.close == thread.waitForAll
|
||||
local count = 0
|
||||
for h in pairs(p.data.handles) do
|
||||
for _,h in ipairs(p.data.handles) do
|
||||
local mt = getmetatable(h)
|
||||
if mt and mt.__status then
|
||||
count = count + 1
|
||||
@ -55,7 +55,7 @@ local cols =
|
||||
end},
|
||||
{"PARENT", function(_,p)
|
||||
for _,process_info in pairs(process.list) do
|
||||
for handle in pairs(process_info.data.handles) do
|
||||
for i,handle in ipairs(process_info.data.handles) do
|
||||
local mt = getmetatable(handle)
|
||||
if mt and mt.__status then
|
||||
if mt.process == p then
|
||||
@ -67,12 +67,7 @@ local cols =
|
||||
return thread_id(nil, p.parent)
|
||||
end},
|
||||
{"HANDLES", function(_, p)
|
||||
local count = 0
|
||||
for _,closure in pairs(p.data.handles) do
|
||||
if closure then
|
||||
count = count + 1
|
||||
end
|
||||
end
|
||||
local count = #p.data.handles
|
||||
return count == 0 and "-" or tostring(count)
|
||||
end},
|
||||
{"CMD", function(_,p) return p.command end},
|
||||
|
@ -76,7 +76,7 @@ local fs_open = fs.open
|
||||
fs.open = function(...)
|
||||
local fs_open_result = table.pack(fs_open(...))
|
||||
if fs_open_result[1] then
|
||||
process.closeOnExit(fs_open_result[1])
|
||||
process.addHandle(fs_open_result[1])
|
||||
end
|
||||
return table.unpack(fs_open_result, 1, fs_open_result.n)
|
||||
end
|
||||
|
@ -166,7 +166,7 @@ function buffer:write(...)
|
||||
if self.bufferMode == "no" then
|
||||
result, reason = self.stream:write(arg)
|
||||
else
|
||||
result, reason = self:buffered_write(arg)
|
||||
result, reason = buffer.buffered_write(self, arg)
|
||||
end
|
||||
|
||||
if not result then
|
||||
|
@ -2,6 +2,7 @@ local text = require("text")
|
||||
local tx = require("transforms")
|
||||
local unicode = require("unicode")
|
||||
local process = require("process")
|
||||
local buffer = require("buffer")
|
||||
|
||||
-- separate string value into an array of words delimited by whitespace
|
||||
-- groups by quotes
|
||||
@ -179,9 +180,7 @@ function text.internal.reader(txt, mode)
|
||||
return true
|
||||
end,
|
||||
}, {__index=text.internal.stream_base((mode or ""):match("b"))})
|
||||
process.closeOnExit(reader)
|
||||
|
||||
return require("buffer").new((mode or "r"):match("[rb]+"), reader)
|
||||
return process.addHandle(buffer.new((mode or "r"):match("[rb]+"), reader))
|
||||
end
|
||||
|
||||
function text.internal.writer(ostream, mode, append_txt)
|
||||
@ -221,9 +220,7 @@ function text.internal.writer(ostream, mode, append_txt)
|
||||
return true
|
||||
end,
|
||||
}, {__index=text.internal.stream_base((mode or ""):match("b"))})
|
||||
process.closeOnExit(writer)
|
||||
|
||||
return require("buffer").new((mode or "w"):match("[awb]+"), writer)
|
||||
return process.addHandle(buffer.new((mode or "w"):match("[awb]+"), writer))
|
||||
end
|
||||
|
||||
function text.detab(value, tabWidth)
|
||||
|
@ -70,14 +70,12 @@ function require(module)
|
||||
end
|
||||
|
||||
function package.delay(lib, file)
|
||||
local mt = {
|
||||
__index = function(tbl, key)
|
||||
setmetatable(lib, nil)
|
||||
setmetatable(lib.internal or {}, nil)
|
||||
dofile(file)
|
||||
return tbl[key]
|
||||
end
|
||||
}
|
||||
local mt = {}
|
||||
function mt.__index(tbl, key)
|
||||
mt.__index = nil
|
||||
dofile(file)
|
||||
return tbl[key]
|
||||
end
|
||||
if lib.internal then
|
||||
setmetatable(lib.internal, mt)
|
||||
end
|
||||
|
@ -133,7 +133,7 @@ function pipe.buildPipeChain(progs)
|
||||
local piped_stream
|
||||
if i < #progs then
|
||||
local handle = setmetatable({buffer = ""}, {__index = pipe_stream})
|
||||
process.closeOnExit(handle, proc)
|
||||
process.addHandle(handle, proc)
|
||||
piped_stream = buffer.new("rw", handle)
|
||||
piped_stream:setvbuf("no", 1024)
|
||||
pio[1] = piped_stream
|
||||
|
@ -131,12 +131,11 @@ function process.internal.close(thread, result)
|
||||
checkArg(1,thread,"thread")
|
||||
local pdata = process.info(thread).data
|
||||
pdata.result = result
|
||||
local handles = {}
|
||||
for s,_ in pairs(pdata.handles) do
|
||||
table.insert(handles, s)
|
||||
end
|
||||
for _,v in ipairs(handles) do
|
||||
pcall(v.close, v)
|
||||
while pdata.handles[1] do
|
||||
local h = table.remove(pdata.handles)
|
||||
if h.close then
|
||||
pcall(h.close, h)
|
||||
end
|
||||
end
|
||||
process.list[thread] = nil
|
||||
end
|
||||
@ -156,20 +155,30 @@ function process.internal.continue(co, ...)
|
||||
return table.unpack(result, 2, result.n)
|
||||
end
|
||||
|
||||
function process.closeOnExit(stream, proc)
|
||||
function process.removeHandle(handle, proc)
|
||||
local handles = (proc or process.info()).data.handles
|
||||
if not handles[stream] then
|
||||
handles[stream] = stream.close
|
||||
stream.close = function(...)
|
||||
local close = handles[stream]
|
||||
handles[stream] = nil
|
||||
if close then
|
||||
return close(...)
|
||||
end
|
||||
for pos, h in ipairs(handles) do
|
||||
if h == handle then
|
||||
return table.remove(handles, pos)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function process.addHandle(handle, proc)
|
||||
local _close = handle.close
|
||||
local handles = (proc or process.info()).data.handles
|
||||
table.insert(handles, handle)
|
||||
function handle:close(...)
|
||||
if _close then
|
||||
self.close = _close
|
||||
_close = nil
|
||||
process.removeHandle(self, proc)
|
||||
return self:close(...)
|
||||
end
|
||||
end
|
||||
return handle
|
||||
end
|
||||
|
||||
function process.running(level) -- kept for backwards compat, prefer process.info
|
||||
local info = process.info(level)
|
||||
if info then
|
||||
|
@ -114,7 +114,7 @@ function box_thread:attach(parent)
|
||||
if mt.attached then
|
||||
-- registration happens on the attached proc, unregister before reparenting
|
||||
waiting_handler = mt.unregister()
|
||||
mt.attached.data.handles[self] = nil
|
||||
process.removeHandle(self, mt.attached)
|
||||
end
|
||||
|
||||
-- fix close
|
||||
@ -122,7 +122,7 @@ function box_thread:attach(parent)
|
||||
|
||||
-- attach to parent or the current process
|
||||
mt.attached = proc
|
||||
process.closeOnExit(self, proc)
|
||||
process.addHandle(self, proc)
|
||||
|
||||
-- register on the new parent
|
||||
if waiting_handler then -- event-waiting
|
||||
@ -137,7 +137,7 @@ function thread.current()
|
||||
local thread_root
|
||||
while proc do
|
||||
if thread_root then
|
||||
for handle in pairs(proc.data.handles) do
|
||||
for _,handle in ipairs(proc.data.handles) do
|
||||
if handle.pco and handle.pco.root == thread_root then
|
||||
return handle
|
||||
end
|
||||
@ -264,7 +264,7 @@ function thread.create(fp, ...)
|
||||
function mt.close()
|
||||
local old_status = t:status()
|
||||
mt.__status = "dead"
|
||||
mt.attached.data.handles[t] = nil
|
||||
process.removeHandle(t, mt.attached)
|
||||
if old_status ~= "dead" then
|
||||
event.push("thread_exit")
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user