Skip to content

Prevent faulty third-party plugins to crash the server #154

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

Merged
merged 3 commits into from
Jan 31, 2022

Conversation

arian-f
Copy link
Contributor

@arian-f arian-f commented Jan 12, 2022

I'm using this in spyder. There on loading pylsp-black, I get this error:

  File "C:\Program Files\Python39\lib\site-packages\pylsp_jsonrpc\endpoint.py", line 116, in consume
    self._handle_request(message['id'], message['method'], message.get('params'))
  File "C:\Program Files\Python39\lib\site-packages\pylsp_jsonrpc\endpoint.py", line 185, in _handle_request
    handler_result = handler(params)
  File "C:\Program Files\Python39\lib\site-packages\pylsp_jsonrpc\dispatchers.py", line 25, in handler
    return method(**(params or {}))
  File "C:\Program Files\Python39\lib\site-packages\pylsp\python_lsp.py", line 210, in m_initialize
    self.config = config.Config(rootUri, initializationOptions or {},
  File "C:\Program Files\Python39\lib\site-packages\pylsp\config\config.py", line 52, in __init__
    entry_point.load()
  File "C:\Program Files\Python39\lib\site-packages\pkg_resources\__init__.py", line 2449, in load
    self.require(*args, **kwargs)
  File "C:\Program Files\Python39\lib\site-packages\pkg_resources\__init__.py", line 2472, in require
    items = working_set.resolve(reqs, env, installer, extras=self.extras)
  File "C:\Program Files\Python39\lib\site-packages\pkg_resources\__init__.py", line 772, in resolve
    raise DistributionNotFound(req, requirers)

pkg_resources.DistributionNotFound: The 'toml' distribution was not found and is required by the application

Which prevents LSP to start (in the status bar, LSP starting spins indefenitely)
not sure why it's not the expected ImportError, but when also catching the pkg_resources.DistributionNotFound spyder seems to work correctly minus the LSP plugin.
Not sure whether this is the way to go, but it seems to me in the spirit of the context, that is catching plugin's errors on loading.

@ccordoba12
Copy link
Member

Hey @arian-f, thanks a lot for your contribution! I think this is an error in one of the PyLSP plugins, so it shouldn't be solved this way, but in the plugin itself.

Could you post the output of pip list to check what plugins you have installed?

@arian-f
Copy link
Contributor Author

arian-f commented Jan 13, 2022

yes, it's caused by pylsp-black

2022-01-13 00:02:48,874 W. Europe Standard Time - WARNING - pylsp.config.config - Failed to load pylsp entry point 'pylsp_black': The 'toml' distribution was not found and is required by the application

@arian-f
Copy link
Contributor Author

arian-f commented Jan 13, 2022

if you decide not to handle this kind of exception, it may still be beneficial to catch any exceptions there, make a warning of the offending plugin and then reraise the excpetion. As you see in the trace above, the offending plugin is not in the trace but rather it goes to pkg_resources. pkg_resources indicates a problem with toml, but the problem may as well be with pylsp-black not correctly specifying its dependencies. (Hope it's clear what's meant)

With the exception caught the offending plugin is printed.

@ccordoba12
Copy link
Member

if you decide not to handle this kind of exception, it may still be beneficial to catch any exceptions there, make a warning of the offending plugin and then reraise the excpetion

I think that's a great idea! But instead of re-raising the exception, we should simply log it (as we're doing right now), so the server doesn't crash due to a faulty plugin.

@arian-f, could you take care of that?

@ccordoba12 ccordoba12 added this to the v1.4.0 milestone Jan 31, 2022
@arian-f
Copy link
Contributor Author

arian-f commented Jan 31, 2022

But instead of re-raising the exception, we should simply log it (as we're doing right now), so the server doesn't crash due to a faulty plugin.

I think I misunderstand your meaning: you're not saying spyder should catch the exception and log it?
because that's what this PR does. Have a look at the change. (Not meant offensively in any way, I just want to make sure this PR isn't already doing what you mean)

catch all exceptions to log them instead of specific exceptions.

Co-authored-by: Carlos Cordoba <ccordoba12@gmail.com>
@arian-f
Copy link
Contributor Author

arian-f commented Jan 31, 2022

right, I merged your change.

@ccordoba12 ccordoba12 changed the title on plugin loading also catch pkg_resources.DistributionNotFound Prevent faulty third-party plugins to crash the server Jan 31, 2022
Co-authored-by: Carlos Cordoba <ccordoba12@gmail.com>
Copy link
Member

@ccordoba12 ccordoba12 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your help with this one @arian-f!

@ccordoba12
Copy link
Member

Note: After a careful look, I think issue #153 is not solved by this.

@ccordoba12 ccordoba12 merged commit 6c5f44d into python-lsp:develop Jan 31, 2022
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

Successfully merging this pull request may close these issues.

2 participants