Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Job keeps running after neovim exits #328

Open
will opened this issue Mar 10, 2022 · 4 comments
Open

Job keeps running after neovim exits #328

will opened this issue Mar 10, 2022 · 4 comments

Comments

@will
Copy link

will commented Mar 10, 2022

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?

@ErikReider
Copy link

I'm experiencing the same issue...

@ErikReider
Copy link

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 })

@zapling
Copy link

zapling commented Dec 14, 2022

Also experienced this issue, even though I called shutdown() on the job. Killing the pid manually on VimLeavePre as mentioned above fixed my issue.

@miversen33
Copy link
Contributor

I've found that there is a bit of an "issue" in how luv (the system used for most shell processing in neovim) handles shutdown down running processes.

I ran into some orphan issues where I would close the handle to the process but the process kept running anyway. This was in my own wrapper for libuv's spawn handler, but it sounds like Plenary may also suffer from similar issues.

My resolution was to have my shutdown function actually execute a uv.kill on the process pid

Note, my issue was related to backgrounded processes not foreground processes, so I am not sure how applicable this is to Plenary. But it might help give an idea (for the maintainers of plenary) on how to address this kind of issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants