plenary.nvim icon indicating copy to clipboard operation
plenary.nvim copied to clipboard

Job keeps running after neovim exits

Open will opened this issue 3 years ago • 4 comments
trafficstars

I have an external program I want to run that runs forever, and very rarely prints a thing on stdout I want to handle.

This works all works fine

local Job = require("plenary.job")

local j = Job:new({
  command = "/path/to/my/thing",
  on_stdout = function(_, msg)
    process(msg)
    end
  end,
})

j:start()

and starts the external process with the parent being neovim as I would expect.

~> ps -l -p 43471,43782
  UID   PID  PPID        F CPU PRI NI       SZ    RSS WCHAN     S             ADDR TTY           TIME CMD
  501 43471 43334     4006   0  31  0  5525556  42416 -      S+                  0 ttys000    0:01.56 nvim test.lua
  501 43782 43471     4006   0  31  0  4560568   9424 -      S+                  0 ttys000    0:00.05 /path/to/my/thing

However, when I quit neovim, the external process is not stopped, and it's parent has changed to 1

~> ps -l -p 43471,43782
  UID   PID  PPID        F CPU PRI NI       SZ    RSS WCHAN     S             ADDR TTY           TIME CMD
  501 43782     1     4006   0  31  0  4560568   9424 -      S                   0 ttys000    0:00.05 /path/to/my/thing

I'm not using the detach flag on job, so I expected the process to die when the parent process died.

I suppose I could register an autocomand to do something at vim exit (maybe? I'm just assuming there is an autocommand for that but don’t know what it is or if it exists for real), but that seems hacky. Is there another way to get this process to automatically exit when neovim itself exists?

will avatar Mar 10 '22 17:03 will

I'm experiencing the same issue...

ErikReider avatar Apr 29 '22 23:04 ErikReider

My hacky solution was to get the pid and run

local function on_done()
    local _handle = io.popen("kill " .. pid)
    if _handle ~= nil then
        _handle:close()
    end
end

vim.api.nvim_create_autocmd("VimLeavePre", { callback = on_done })

ErikReider avatar Apr 29 '22 23:04 ErikReider