Skip to content

Commit

Permalink
red: pipe cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hugeping committed Oct 25, 2023
1 parent 75a73af commit a0598da
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
5 changes: 1 addition & 4 deletions data/lib/red/proc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,7 @@ local function piped(w, out, prog)
txt = txt:sub(257)
coroutine.yield(true)
end
if ret.fifo then
ret.fifo:close()
ret.fifo = nil
end
ret:close()
end)
end

Expand Down
43 changes: 26 additions & 17 deletions data/lib/red/shell.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,25 @@ local function pipe_proc()
f:close()
thread:write '\1eof'
end
local pipe = {}
pipe.__index = pipe

function pipe:close()
if not self.fifo then
return
end
self.fifo:close()
self.fifo = nil
end

function pipe:kill()
if self.stopped then
return
end
self.thread:err("kill")
self.thread:detach()
self.stopped = true
end

function shell.pipe(w, prog, inp, sh)
local tmp
Expand All @@ -90,6 +109,7 @@ function shell.pipe(w, prog, inp, sh)
end
local p = thread.start(sh and pipe_shell or pipe_proc)
local ret = { }
setmetatable(ret, pipe)
p:write(prog, w.cwd or false)
local r, e = p:read()
if not r then
Expand Down Expand Up @@ -133,23 +153,14 @@ function shell.pipe(w, prog, inp, sh)
os.remove(tmp)
end
ret.stopped = true
if ret.fifo then
ret.fifo:close()
ret.fifo = nil
end
ret:close()
p:wait()
end)
ret.routine = r
ret.thread = p
r.kill = function()
if ret.fifo then
ret.fifo:close()
ret.fifo = nil
end
if not ret.stopped then
p:err("kill")
p:detach()
ret.stopped = true
end
ret:close()
ret:kill()
end
return ret
end
Expand All @@ -160,17 +171,15 @@ function shell:delete()
self.prog.stopped then
return
end
self.prog.routine.kill()
self.prog.stopped = true
self.prog:kill()
end

function shell:escape()
if not self.prog or self.prog.stopped or
not self.prog.fifo then
return self.super.escape(self)
end
self.prog.fifo:close()
self.prog.fifo = nil
self.prog:close()
end

function shell:prompt()
Expand Down

0 comments on commit a0598da

Please sign in to comment.