Skip to content

Commit

Permalink
fix: close spawn handler (#209)
Browse files Browse the repository at this point in the history
* fix: spawn close handler

* perf: set hide=true for spawn
  • Loading branch information
linrongbin16 authored Sep 27, 2023
1 parent d6ed382 commit 0338d6d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 80 deletions.
25 changes: 2 additions & 23 deletions bin/general/previewer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,34 +104,13 @@ elseif metajson.previewer_type == "command_list" then
return
end

local process_context = {
process_handler = nil,
process_id = nil,
}
local async_spawn = shell_helpers.AsyncSpawn:open(cmd_splits, println, {
on_exit = function(code, signal)
vim.loop.stop()
if shell_helpers.is_windows then
if process_context.process_handler then
process_context.process_handler:kill()
end
else
if process_context.process_id then
vim.loop.kill(process_context.process_id --[[@as integer]])
end
end
os.exit(code)
end,
}) --[[@as AsyncSpawn]]
local async_spawn = shell_helpers.AsyncSpawn:open(cmd_splits, println) --[[@as AsyncSpawn]]
shell_helpers.log_ensure(
async_spawn ~= nil,
"|provider| error! failed to open async command: %s",
vim.inspect(cmd_splits)
)
local process_handler, process_id = async_spawn:start()
process_context.process_handler = process_handler
process_context.process_id = process_id
vim.loop.run()
async_spawn:run()
elseif metajson.previewer_type == "list" then
local f = io.open(resultfile, "r")
shell_helpers.log_ensure(
Expand Down
25 changes: 2 additions & 23 deletions bin/general/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,34 +114,13 @@ then
return
end

local process_context = {
process_handler = nil,
process_id = nil,
}
local async_spawn = shell_helpers.AsyncSpawn:open(cmd_splits, println, {
on_exit = function(code, signal)
vim.loop.stop()
if shell_helpers.is_windows then
if process_context.process_handler then
process_context.process_handler:kill()
end
else
if process_context.process_id then
vim.loop.kill(process_context.process_id --[[@as integer]])
end
end
os.exit(code)
end,
}) --[[@as AsyncSpawn]]
local async_spawn = shell_helpers.AsyncSpawn:open(cmd_splits, println) --[[@as AsyncSpawn]]
shell_helpers.log_ensure(
async_spawn ~= nil,
"|provider| error! failed to open async command: %s",
vim.inspect(cmd_splits)
)
local process_handler, process_id = async_spawn:start()
process_context.process_handler = process_handler
process_context.process_id = process_id
vim.loop.run()
async_spawn:run()
elseif metaopts.provider_type == "list" then
local reader = shell_helpers.FileLineReader:open(resultfile) --[[@as FileLineReader ]]
shell_helpers.log_ensure(
Expand Down
71 changes: 38 additions & 33 deletions lua/fzfx/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,16 @@ local ShellOptsContext = {}
--- @return ShellOptsContext
function ShellOptsContext:save()
local o = constants.is_windows
and {
shell = vim.o.shell,
shellslash = vim.o.shellslash,
shellcmdflag = vim.o.shellcmdflag,
shellxquote = vim.o.shellxquote,
shellquote = vim.o.shellquote,
shellredir = vim.o.shellredir,
shellpipe = vim.o.shellpipe,
shellxescape = vim.o.shellxescape,
}
and {
shell = vim.o.shell,
shellslash = vim.o.shellslash,
shellcmdflag = vim.o.shellcmdflag,
shellxquote = vim.o.shellxquote,
shellquote = vim.o.shellquote,
shellredir = vim.o.shellredir,
shellpipe = vim.o.shellpipe,
shellxescape = vim.o.shellxescape,
}
or {
shell = vim.o.shell,
}
Expand Down Expand Up @@ -387,14 +387,14 @@ end
--- @return integer
function FileLineReader:_read_chunk()
local chunksize = (self.filesize >= self.offset + self.batchsize)
and self.batchsize
and self.batchsize
or (self.filesize - self.offset)
if chunksize <= 0 then
return 0
end
local data, --[[@as string?]]
read_err,
read_name =
read_err,
read_name =
vim.loop.fs_read(self.handler, chunksize, self.offset)
if read_err then
error(
Expand Down Expand Up @@ -526,6 +526,8 @@ end
--- @field out_pipe uv_pipe_t
--- @field err_pipe uv_pipe_t
--- @field out_buffer string?
--- @field process_handler uv_process_t?
--- @field process_id integer|string|nil
--- @field opts AsyncSpawnRunOpts?
local AsyncSpawn = {}

Expand All @@ -546,6 +548,8 @@ function AsyncSpawn:open(cmds, fn_line_consumer, opts)
out_pipe = out_pipe,
err_pipe = err_pipe,
out_buffer = nil,
process_handler = nil,
process_id = nil,
opts = opts,
}
setmetatable(o, self)
Expand All @@ -570,6 +574,20 @@ function AsyncSpawn:consume_line(buffer, fn_line_processor)
return i
end

--- @param code integer?
--- @param signal integer?
--- @return nil
function AsyncSpawn:on_exit(code, signal)
if self.process_handler and not self.process_handler:is_closing() then
self.process_handler:close(function(err)
vim.loop.stop()
end)
end
if type(self.opts) == "table" and type(self.opts.on_exit) == "function" then
self.opts.on_exit(code, signal)
end
end

--- @param err string?
--- @param data string?
--- @return nil
Expand Down Expand Up @@ -599,7 +617,7 @@ function AsyncSpawn:on_stdout(err, data)
local i = self:consume_line(self.out_buffer, self.fn_line_consumer)
-- truncate the printed lines if found any
self.out_buffer = i <= #self.out_buffer
and self.out_buffer:sub(i, #self.out_buffer)
and self.out_buffer:sub(i, #self.out_buffer)
or nil
end

Expand All @@ -619,26 +637,11 @@ function AsyncSpawn:on_stderr(err, data)
end
end

--- @param code integer?
--- @param signal integer?
--- @return nil
function AsyncSpawn:on_exit(code, signal)
if not self.out_pipe:is_closing() then
self.out_pipe:close()
end
if not self.err_pipe:is_closing() then
self.err_pipe:close()
end
if type(self.opts) == "table" and type(self.opts.on_exit) == "function" then
self.opts.on_exit(code, signal)
end
end

--- @return uv_process_t?, string|integer
function AsyncSpawn:start()
function AsyncSpawn:run()
local process_handler, process_id = vim.loop.spawn(self.cmds[1], {
args = vim.list_slice(self.cmds, 2),
stdio = { nil, self.out_pipe, self.err_pipe },
hide = true,
-- verbatim = true,
}, function(code, signal)
self.out_pipe:read_stop()
Expand All @@ -648,14 +651,16 @@ function AsyncSpawn:start()
self:on_exit(code, signal)
end)

self.process_handler = process_handler
self.process_id = process_id

self.out_pipe:read_start(function(err, data)
self:on_stdout(err, data)
end)
self.err_pipe:read_start(function(err, data)
self:on_stderr(err, data)
end)

return process_handler, process_id
vim.loop.run()
end

local M = {
Expand Down
34 changes: 33 additions & 1 deletion test/utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ describe("utils", function()

local i = 1
local function process_line(line)
print(string.format("[%d]%s\n", i, line))
-- print(string.format("[%d]%s\n", i, line))
assert_eq(type(line), "string")
assert_eq(line, lines[i])
i = i + 1
Expand All @@ -429,5 +429,37 @@ describe("utils", function()
) --[[@as AsyncSpawn]]
async_spawn:on_stderr(nil, nil)
end)
it("iterate on README.md", function()
local lines = utils.readlines("README.md") --[[@as table]]

local i = 1
local function process_line(line)
print(string.format("[%d]%s\n", i, line))
assert_eq(type(line), "string")
assert_eq(lines[i], line)
i = i + 1
end

local async_spawn =
utils.AsyncSpawn:open({ "cat", "README.md" }, process_line) --[[@as AsyncSpawn]]
async_spawn:run()
end)
it("iterate on lua/fzfx/config.lua", function()
local lines = utils.readlines("lua/fzfx/config.lua") --[[@as table]]

local i = 1
local function process_line(line)
print(string.format("[%d]%s\n", i, line))
assert_eq(type(line), "string")
assert_eq(lines[i], line)
i = i + 1
end

local async_spawn = utils.AsyncSpawn:open(
{ "cat", "lua/fzfx/config.lua" },
process_line
) --[[@as AsyncSpawn]]
async_spawn:run()
end)
end)
end)

0 comments on commit 0338d6d

Please sign in to comment.