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 8 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
2 changes: 1 addition & 1 deletion requirements/conda.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pygments >=2.0
pylint
pympler
pyqt <5.13
python-language-server >=0.28.2
python-language-server >=0.28.3
ccordoba12 marked this conversation as resolved.
Show resolved Hide resolved
pyxdg
pyzmq
qdarkstyle >=2.7
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def run(self):
# pyqtwebengine module
'pyqtwebengine<5.13;python_version>="3"',
# Pyls with all its dependencies
'python-language-server[all]>=0.28.2,<0.29.0',
'python-language-server[all]>=0.28.3,<0.29.0',
# Required to get SSH connections to remote kernels
'pexpect',
'paramiko;platform_system=="Windows"',
Expand Down
3 changes: 2 additions & 1 deletion spyder/plugins/completion/languageserver/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,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
5 changes: 3 additions & 2 deletions spyder/plugins/completion/languageserver/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,10 @@ def generate_python_config(self):
# Setup options in json
python_config['cmd'] = cmd
if host in self.LOCALHOST and not stdio:
python_config['args'] = '--host {host} --port {port} --tcp'
python_config['args'] = ('--host {host} --port {port} --tcp '
'--check-parent-process')
else:
python_config['args'] = ''
python_config['args'] = '--check-parent-process'
python_config['external'] = external_server
python_config['stdio'] = stdio
python_config['host'] = host
Expand Down
30 changes: 28 additions & 2 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,12 +137,33 @@ def restore(self):
client = LanguageServerClient(zmq_in_port=args.zmq_in_port,
zmq_out_port=args.zmq_out_port)
client.start()
is_alive = True

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

Code taken from the Python Language Server project.
"""
global is_alive
if not psutil.pid_exists(pid):
logger.info("parent process %s is not alive, exiting!", pid)
is_alive = False
if is_alive:
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:
while is_alive:
client.listen()
except TerminateSignal:
pass
client.stop()
# sig_manager.restore()

process.terminate()
process.wait()
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def stop(self):
logger.info('Closing consumer thread...')
self.reading_thread.stop()
logger.debug('Joining thread...')
self.reading_thread.join()
logger.debug('Exit routine should be complete')

def transport_send(self, content_length, body):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ def stop(self):
self.socket.close()
logger.info('Closing consumer thread...')
self.reading_thread.stop()
logger.debug('Joining thread...')
self.reading_thread.join()
logger.debug('Exit routine should be complete')

def transport_send(self, content_length, body):
Expand Down