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

fix(job): remove freeze after shutdown is called

Open marcuscaisey opened this issue 3 years ago • 0 comments

Context

I use both plenary.popup and plenary.job in my plugin please.nvim to run a command in a popup terminal and map q to close the popup / shutdown the job like this (simplified, actual code here):

vim.keymap.set('n', 'q', function()
  close_windows()
  job:shutdown()
end, { buffer = term_bufnr })

Problem

The editor becomes unresponsive for a second after pressing q (and therefore calling Job:shutdown). Here's a minimal example which demonstrates this:

local Job = require 'plenary.job'
local job = Job:new {
  command = 'bash',
  args = { '-c', 'for i in $(seq 1 1000); do echo line $i && sleep 0.1; done' },
}

vim.keymap.set('n', 'q', function()
  job:shutdown()
end, { buffer = vim.api.nvim_get_current_buf() })

job:start()

If you :so % this and then q, you won't be able to move the cursor up and down for a second. This is because in Job:shutdown we wait for 1000ms, checking every 1ms, for self:_pipes_are_closed(self) and self.is_shutdown which never happens: https://github.com/nvim-lua/plenary.nvim/blob/4b66054e75356ac0b909bbfee9c682e703f535c2/lua/plenary/job.lua#L213-L221

Solution

Just remove the vim.wait call since i'm not sure what purpose it serves. Unless the command that you're running finishes in the time between the Job:shutdown call and 1s after that, self:_pipes_are_closed(self) and self._is_shutdown is always going to be false and so we're always going to wait / freeze for 1s. Please correct me if there is a function of the vim.wait that i'm missing 🙏

marcuscaisey avatar Sep 11 '22 23:09 marcuscaisey