vim-vdebug / vdebug

Multi-language DBGP debugger client for Vim (PHP, Python, Perl, Ruby, etc.)
MIT License
1.3k stars 156 forks source link

Requests from different threads should be wrapped with nvim.async_call(cb, ...) #426

Closed doronbehar closed 4 years ago

doronbehar commented 5 years ago

I use Komodo's pydbgp in order to debug Python scripts. I get the following harmless error when stopping the debugger:

Request from non-main thread.
Requests from different threads should be wrapped with nvim.async_call(cb, ...)
  File "/home/doron/.config/nvim/pack/functional/start/vdebug/python3/vdebug/connection.py", line 199, in run
    print("Exception caught")
  File "/nix/store/as3nq2kzs8l9jsmhisq1l97rdbchzngg-python3-3.7.4-env/lib/python3.7/site-packages/pynvim/plugin/script_host.py", line 182, in write
    self.redirect_handler(data)
  File "/nix/store/as3nq2kzs8l9jsmhisq1l97rdbchzngg-python3-3.7.4-env/lib/python3.7/site-packages/pynvim/plugin/script_host.py", line 75, in <lambda>
    sys.stdout = RedirectStream(lambda data: nvim.out_write(data))
  File "/nix/store/as3nq2kzs8l9jsmhisq1l97rdbchzngg-python3-3.7.4-env/lib/python3.7/site-packages/pynvim/api/nvim.py", line 393, in out_write
    return self.request('nvim_out_write', msg, **kwargs)
Exception in thread Thread-7:
Traceback (most recent call last):
  File "/home/doron/.config/nvim/pack/functional/start/vdebug/python3/vdebug/connection.py", line 183, in run
    self.__peek_for_exit()
  File "/home/doron/.config/nvim/pack/functional/start/vdebug/python3/vdebug/connection.py", line 209, in __peek_for_exit
    self.__check_exit(self.__message_q.get_nowait())
  File "/home/doron/.config/nvim/pack/functional/start/vdebug/python3/vdebug/connection.py", line 216, in __check_exit
    raise Exception("Exiting")
Exception: Exiting
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/nix/store/zjzq4qp81z8qnnnchnkycmsq0impw173-python3-3.7.4/lib/python3.7/threading.py", line 926, in _bootstrap_inner
    self.run()
  File "/home/doron/.config/nvim/pack/functional/start/vdebug/python3/vdebug/connection.py", line 199, in run
    print("Exception caught")
  File "/nix/store/as3nq2kzs8l9jsmhisq1l97rdbchzngg-python3-3.7.4-env/lib/python3.7/site-packages/pynvim/plugin/script_host.py", line 182, in write
    self.redirect_handler(data)
  File "/nix/store/as3nq2kzs8l9jsmhisq1l97rdbchzngg-python3-3.7.4-env/lib/python3.7/site-packages/pynvim/plugin/script_host.py", line 75, in <lambda>
    sys.stdout = RedirectStream(lambda data: nvim.out_write(data))
  File "/nix/store/as3nq2kzs8l9jsmhisq1l97rdbchzngg-python3-3.7.4-env/lib/python3.7/site-packages/pynvim/api/nvim.py", line 393, in out_write
    return self.request('nvim_out_write', msg, **kwargs)
  File "/nix/store/as3nq2kzs8l9jsmhisq1l97rdbchzngg-python3-3.7.4-env/lib/python3.7/site-packages/pynvim/api/nvim.py", line 178, in request
    raise NvimError("request from non-main thread")
pynvim.api.nvim.NvimError: request from non-main thread
Vdebug stopped waiting for a connection

Not sure if it's easily fixable but I hope my report is useful.

EDIT: Thanks for creating this Vim Plugin BTW :) It seems very promising and much better then the very few alternatives.

meldafert commented 4 years ago

The problem is the print call in BackgroundSocketCreator.run(), as print communicates with vim/nvim, and nvim appears to only allow that from the main thread.

One way to fix the common case would be to not use an Exception when only exiting; however, in the case where an actual Exception might happen, it would still fail at the print and not actually report the exception.

When testing with Vim8, it appears as if the print call is just ignored if it does not happen in the main thread, so it would be the best to remove it altogether (and the second print call that happens in this function), as those would only be ignored in vim and throw an error in nvim, and both have proper logging set up either way.

(As an aside: It is possible to hook up vim.async_call for nvim; however, this then prints a rather ugly and useless "Exception caught" message to the status bar, hence my assumption that this print call should not be there in the first place, and that it is not worth using async_call, which only works with nvim.)

cprn commented 4 years ago

edit: Nevermind, my error was due to other service using port 9000, bound both xdebug and vdebug to 9690 instead and it worked. Leaving this comment here in case someone googles the error for the same reason.


Hi. Please confirm it's the same bug and if so, raise the severity, because this happens when I try to run :VdebugStart:

Request from non-main thread.                                                                                                                                                                                                         
Requests from different threads should be wrapped with nvim.async_call(cb, ...) 
  File "/home/mg/.config/nvim/plugged/vdebug/python3/vdebug/connection.py", line 197, in run
    print("Socket is already in use")
  File "/home/mg/.local/lib/python3.6/site-packages/pynvim/plugin/script_host.py", line 182, in write
    self.redirect_handler(data)
  File "/home/mg/.local/lib/python3.6/site-packages/pynvim/plugin/script_host.py", line 75, in <lambda>
    sys.stdout = RedirectStream(lambda data: nvim.out_write(data))
  File "/home/mg/.local/lib/python3.6/site-packages/pynvim/api/nvim.py", line 396, in out_write
    return self.request('nvim_out_write', msg, **kwargs)
Press ENTER or type command to continue

And after pressing enter:

Exception in thread Thread-6:
Traceback (most recent call last):
  File "/home/mg/.config/nvim/plugged/vdebug/python3/vdebug/connection.py", line 178, in run
    s.bind((self.__host, self.__port))
OSError: [Errno 98] Address already in use
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/home/mg/.config/nvim/plugged/vdebug/python3/vdebug/connection.py", line 197, in run
    print("Socket is already in use")
  File "/home/mg/.local/lib/python3.6/site-packages/pynvim/plugin/script_host.py", line 182, in write
    self.redirect_handler(data)
  File "/home/mg/.local/lib/python3.6/site-packages/pynvim/plugin/script_host.py", line 75, in <lambda>
    sys.stdout = RedirectStream(lambda data: nvim.out_write(data))
  File "/home/mg/.local/lib/python3.6/site-packages/pynvim/api/nvim.py", line 396, in out_write
    return self.request('nvim_out_write', msg, **kwargs)
  File "/home/mg/.local/lib/python3.6/site-packages/pynvim/api/nvim.py", line 178, in request
    raise NvimError("request from non-main thread")
pynvim.api.common.NvimError: request from non-main thread
Press ENTER or type command to continue

Side question: how do I downgrade to get it working? Using vim-plug.

meldafert commented 4 years ago

@cprn This is indeed related, since without this error you would have gotten a simple "Socket is already in use" message.

BlackIkeEagle commented 4 years ago

should be fixed with #479

meldafert commented 4 years ago

Note that the "Socket is already in use" error message will still trigger the nvim.async_call(cb, ...) issue on neovim. Since this is an error case, this is far less severe, especially since the relevant error message will be shown - if buried in stacktrace noise.