8000 Prevent third-party plugins with faulty hooks to crash the server · python-lsp/python-lsp-server@d4baee3 · GitHub
[go: up one dir, main page]

Skip to content

Commit d4baee3

Browse files
committed
Prevent third-party plugins with faulty hooks to crash the server
1 parent 6c5f44d commit d4baee3

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

pylsp/config/config.py

Lines changed: 22 additions & 2 deletions
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)

0 commit comments

Comments
 (0)
0