Skip to content
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

PR: Kill LSP transport layer if Spyder gets killed #10111

Merged
merged 17 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion spyder/plugins/completion/languageserver/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,9 @@ def perform_request(self, method, params):
@handles(SERVER_READY)
@send_request(method=LSPRequestTypes.INITIALIZE)
def initialize(self, *args, **kwargs):
pid = self.transport_client.pid if not self.external_server else None
params = {
'processId': self.transport_client.pid,
'processId': pid,
'rootUri': pathlib.Path(osp.abspath(self.folder)).as_uri(),
'capabilities': self.client_capabilites,
'trace': TRACE
Expand Down
25 changes: 25 additions & 0 deletions spyder/plugins/completion/languageserver/transport/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import os
import psutil
import signal
import threading
from functools import partial

# Local imports
Expand All @@ -31,6 +32,8 @@

logger = logging.getLogger(__name__)

PARENT_PROCESS_WATCH_INTERVAL = 3 # 3 s


parser = argparse.ArgumentParser(
description='ZMQ Python-based MS Language-Server v3.0 client for Spyder')
Expand Down Expand Up @@ -120,6 +123,8 @@ def restore(self):
extra_args = ' '.join(extra_args)
logger.debug(extra_args)
process = psutil.Process()
parent_pid = process.ppid()

sig_manager = SignalManager()
if args.stdio_server:
LanguageServerClient = partial(StdioLanguageServerClient,
Expand All @@ -132,6 +137,26 @@ def restore(self):
client = LanguageServerClient(zmq_in_port=args.zmq_in_port,
zmq_out_port=args.zmq_out_port)
client.start()

def watch_parent_process(pid):
"""
Exit when the given pid is not alive.

Code taken from the Python Language Server project.
"""
if not psutil.pid_exists(pid):
logger.info("parent process %s is not alive, exiting!", pid)
client.stop()
process.terminate()
process.wait()
threading.Timer(PARENT_PROCESS_WATCH_INTERVAL, watch_parent_process,
args=[pid]).start()

watching_thread = threading.Thread(
target=watch_parent_process, args=(parent_pid,))
watching_thread.daemon = True
watching_thread.start()

try:
while True:
client.listen()
Expand Down