numirias / semshi

🌈 Semantic Highlighting for Python in Neovim
1.02k stars 32 forks source link

Invalid window id when closing windows #72

Open petobens opened 4 years ago

petobens commented 4 years ago

Consider the following minimal init.vim file:

let $DOTVIM = expand('$HOME/.config/nvim')

set runtimepath+=$DOTVIM/bundle/repos/github.com/numirias/semshi
filetype plugin indent on

Now open nvim and, as in the GIF, do:

  1. Edit some random python file with :e a.py and add a single line # foo.
  2. Split windows with :vsp
  3. Run location list vimgrep with :lvim foo % | lopen
  4. Switch from the location list window to the new split window and run 3. again
  5. Switch from this new location list window to the first (left) window (using <Ctrl-w> mappings)
  6. Run windo if &buftype == 'quickfix' | lclose | endif

After 6. an invalid window id error is triggered:

Error detected while processing function SemshiBufEnter[1]..remote#define#request:
line    2:
error caught in async handler '/home/pedro/git-repos/private/dotfiles/vim/bundle/.cache/init.vim/.dein/rplugin/python3/semshi:function:SemshiCursorMoved [[1, 1]]'
Traceback (most recent call last):
  File "/home/pedro/git-repos/private/dotfiles/vim/bundle/.cache/init.vim/.dein/rplugin/python3/semshi/plugin.py", line 102, in event_cursor_moved
    self._mark_selected()
  File "/home/pedro/git-repos/private/dotfiles/vim/bundle/.cache/init.vim/.dein/rplugin/python3/semshi/plugin.py", line 234, in _mark_selected
    self._cur_handler.mark_selected(self._vim.current.window.cursor)
  File "/home/pedro/.local/lib/python3.8/site-packages/pynvim/api/window.py", line 22, in cursor
    return self.request('nvim_win_get_cursor')
  File "/home/pedro/.local/lib/python3.8/site-packages/pynvim/api/common.py", line 58, in request
    return self._session.request(name, self, *args, **kwargs)
  File "/home/pedro/.local/lib/python3.8/site-packages/pynvim/api/nvim.py", line 182, in request
    res = self._session.request(name, *args, **kwargs)
  File "/home/pedro/.local/lib/python3.8/site-packages/pynvim/msgpack_rpc/session.py", line 104, in request
    raise self.error_wrapper(err)
pynvim.api.common.NvimError: Invalid window id: 1003

semshi

voldikss commented 4 years ago

Ran into this issue when I use asyncrun.vim to run python code in a split terminal window. When I closed the terminal, this error occured.

petobens commented 4 years ago

@numirias hi! Any chance/plans to look into this? Thanks in advance :)

blueyed commented 3 years ago

I am seeing this often with vim-fugitive (https://github.com/tpope/vim-fugitive/issues/1634).

From adding some printing etc it appears the window is gone when Semshi asks for the cursor position there.

DBG: [37, 37] (WinLeave: abuf: 4, amatch: …foo.py, winnr():2, ft: python, bt: )
DBG: [37, 37] (WinEnter: abuf: 16, amatch: fugitive://…, winnr():1, ft: python, bt: )
DBG: [37, 37] (BufEnter: abuf: 16, amatch: fugitive://…, winnr():1, ft: python, bt: )
=== _update_step buf=<Buffer(handle=16)> ===
  File "/usr/lib/python3.8/threading.py", line 890, in _bootstrap
    self._bootstrap_inner()
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "…/vim/plugged/semshi/rplugin/python3/semshi/handler.py", line 158, in _update_loop
    self._update_step()
  File "…/vim/plugged/semshi/rplugin/python3/semshi/util.py", line 19, in wrapper
    res = func(*args, **kwargs)
  File "…/vim/plugged/semshi/rplugin/python3/semshi/handler.py", line 178, in _update_step
    self._buf, "".join(traceback.format_stack())

semshi: _wait_for: (<function BufferHandler._update_step.<locals>.<lambda> at 0x7ff5ae1dd430>, False)
  File "/usr/lib/python3.8/threading.py", line 890, in _bootstrap
    self._bootstrap_inner()
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "…/vim/plugged/semshi/rplugin/python3/semshi/handler.py", line 158, in _update_loop
    self._update_step()
  File "…/vim/plugged/semshi/rplugin/python3/semshi/util.py", line 19, in wrapper
    res = func(*args, **kwargs)
  File "…/vim/plugged/semshi/rplugin/python3/semshi/handler.py", line 184, in _update_step
    code = self._wait_for(lambda: lines_to_code(self._buf[:]), sync)
  File "…/vim/plugged/semshi/rplugin/python3/semshi/handler.py", line 118, in _wait_for
    "semshi: _wait_for", (func, sync), "".join(traceback.format_stack())
semshi: _wait_for: (<function BufferHandler._update_step.<locals>.<lambda> at 0x7ff5ae1ddaf0>, False)
  File "/usr/lib/python3.8/threading.py", line 890, in _bootstrap
    self._bootstrap_inner()
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "…/vim/plugged/semshi/rplugin/python3/semshi/handler.py", line 158, in _update_loop
    self._update_step()
  File "…/vim/plugged/semshi/rplugin/python3/semshi/util.py", line 19, in wrapper
    res = func(*args, **kwargs)
  File "…/vim/plugged/semshi/rplugin/python3/semshi/handler.py", line 206, in _update_step
    cursor = self._wait_for(
  File "…/vim/plugged/semshi/rplugin/python3/semshi/handler.py", line 118, in _wait_for
    "semshi: _wait_for", (func, sync), "".join(traceback.format_stack())
DBG: [37, 37] (BufWinLeave: abuf: 16, amatch: fugitive://…, winnr():1, ft: python, bt: )
DBG: [37, 37] (BufUnload: abuf: 16, amatch: fugitive://…, winnr():1, ft: python, bt: )
DBG: [37, 37] (BufDelete: abuf: 16, amatch: fugitive://…, winnr():1, ft: python, bt: )
DBG: [37] (WinEnter: abuf: 4, amatch: …foo.py, winnr():1, ft: python, bt: )
Error detected while processing function SemshiBufEnter[1]..remote#define#request:
line    2:
error caught while executing async callback:
NvimError('Invalid window id: 1002')
Traceback (most recent call last):
  File "…/vim/plugged/semshi/rplugin/python3/semshi/handler.py", line 124, in wrapper
    res = func()
  File "…/vim/plugged/semshi/rplugin/python3/semshi/handler.py", line 207, in <lambda>
    lambda: self._vim.current.window.cursor, sync
  File "/usr/lib/python3.8/site-packages/pynvim/api/window.py", line 22, in cursor
    return self.request('nvim_win_get_cursor')
  File "/usr/lib/python3.8/site-packages/pynvim/api/common.py", line 58, in request
    return self._session.request(name, self, *args, **kwargs)
  File "/usr/lib/python3.8/site-packages/pynvim/api/nvim.py", line 182, in request
    res = self._session.request(name, *args, **kwargs)
  File "/usr/lib/python3.8/site-packages/pynvim/msgpack_rpc/session.py", line 102, in request
    raise self.error_wrapper(err)
pynvim.api.common.NvimError: Invalid window id: 1002

the call was requested at
  File "…/vim/plugged/semshi/rplugin/python3/semshi/handler.py", line 158, in _update_loop
    self._update_step()
  File "…/vim/plugged/semshi/rplugin/python3/semshi/util.py", line 19, in wrapper
    res = func(*args, **kwargs)
  File "…/vim/plugged/semshi/rplugin/python3/semshi/handler.py", line 206, in _update_step
    cursor = self._wait_for(
  File "…/vim/plugged/semshi/rplugin/python3/semshi/handler.py", line 134, in _wait_for
    self._vim.async_call(wrapper)
error while running <function BufferHandler._update_step.<locals>.<lambda> at 0x7ff5ae1ddaf0>: Invalid window id: 1002

Here the handler (0x7ff5ae1ddaf0) was triggered for BufEnter of buffer 16, and the error happens after WinEnter of buffer 4, when buffer 16 was deleted/unloaded already.

I might look into this a bit more later, but it might also make sense to unregister via BufUnload/BufDelete, and/or just ignore this kind of expected error.

blueyed commented 3 years ago

https://github.com/numirias/semshi/pull/94 should help here / fix this, please give it a try.

wookayin commented 3 years ago

Bumping this up with #94! (Actually similar errors can happen somewhere else, such as self._update_step(self._options.always_update_all_highlights), etc.)