Skip to content

Commit 6042dab

Browse files
authored
Prevent third-party plugins with faulty hooks to crash the server (#158)
1 parent aa6315d commit 6042dab

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

pylsp/config/config.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
import logging
66
from functools import lru_cache
7-
import pkg_resources
7+
from typing import List, Mapping, Sequence, Union
88

9+
import pkg_resources
910
import pluggy
11+
from pluggy._hooks import HookImpl
1012

1113
from pylsp import _utils, hookspecs, uris, PYLSP
1214

@@ -16,6 +18,24 @@
1618
DEFAULT_CONFIG_SOURCES = ['pycodestyle']
1719

1820

21+
class PluginManager(pluggy.PluginManager):
22+
23+
def _hookexec(
24+
self,
25+
hook_name: str,
26+
methods: Sequence[HookImpl],
27+
kwargs: Mapping[str, object],
28+
firstresult: bool,
29+
) -> Union[object, List[object]]:
30+
# called from all hookcaller instances.
31+
# enable_tracing will set its own wrapping function at self._inner_hookexec
32+
try:
33+
return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
34+
except Exception as e: # pylint: disable=broad-except
35+
log.warning(f"Failed to load hook {hook_name}: {e}")
36+
return []
37+
38+
1939
class Config:
2040
def __init__(self, root_uri, init_opts, process_id, capabilities):
2141
self._root_path = uris.to_fs_path(root_uri)
@@ -39,7 +59,7 @@ def __init__(self, root_uri, init_opts, process_id, capabilities):
3959
except ImportError:
4060
pass
4161

42-
self._pm = pluggy.PluginManager(PYLSP)
62+
self._pm = PluginManager(PYLSP)
4363
self._pm.trace.root.setwriter(log.debug)
4464
self._pm.enable_tracing()
4565
self._pm.add_hookspecs(hookspecs)

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ python_requires = >=3.7
1414
install_requires =
1515
jedi>=0.17.2,<0.19.0
1616
python-lsp-jsonrpc>=1.0.0
17-
pluggy
17+
pluggy>=1.0.0
1818
ujson>=3.0.0
1919
setuptools>=39.0.0
2020
setup_requires = setuptools>=44; wheel; setuptools_scm[toml]>=3.4.3

0 commit comments

Comments
 (0)