diff --git a/ptvsd/wrapper.py b/ptvsd/wrapper.py index a9a0ba21e..b2d0c9fe6 100644 --- a/ptvsd/wrapper.py +++ b/ptvsd/wrapper.py @@ -8,6 +8,7 @@ import errno import os import platform +import signal import socket import sys import threading @@ -37,6 +38,7 @@ #ipcjson._TRACE = ipcjson_trace ptvsd_sys_exit_code = 0 +WAIT_FOR_DISCONNECT_REQUEST_TIMEOUT = 2 WAIT_FOR_THREAD_FINISH_TIMEOUT = 1 def unquote(s): @@ -389,7 +391,8 @@ def __init__(self, socket, pydevd, logfile=None): self.next_var_ref = 0 self.loop = futures.EventLoop() self.exceptions_mgr = ExceptionsManager(self) - + self.disconnect_request = None + self.disconnect_request_event = threading.Event() pydevd._vscprocessor = self self._closed = False @@ -413,6 +416,11 @@ def close(self): self.send_event('exited', exitCode=ptvsd_sys_exit_code) self.send_event('terminated') + self.disconnect_request_event.wait(WAIT_FOR_DISCONNECT_REQUEST_TIMEOUT) + if self.disconnect_request is not None: + self.send_response(self.disconnect_request) + self.disconnect_request = None + self.set_exit() self.loop.stop() self.event_loop_thread.join(WAIT_FOR_THREAD_FINISH_TIMEOUT) @@ -509,7 +517,15 @@ def on_configurationDone(self, request, args): def on_disconnect(self, request, args): # TODO: docstring - self.send_response(request) + if self.start_reason == 'launch': + self.disconnect_request = request + self.disconnect_request_event.set() + killProcess = not self._closed + self.close() + if killProcess: + os.kill(os.getpid(), signal.SIGTERM) + else: + self.send_response(request) @async_handler def on_attach(self, request, args): @@ -969,6 +985,10 @@ def exit_handler(proc, server_thread): if server_thread.is_alive(): server_thread.join(WAIT_FOR_THREAD_FINISH_TIMEOUT) +def signal_handler(signum, frame, proc): + proc.close() + sys.exit(0) + def start_server(port): """Return a socket to a (new) local pydevd-handling daemon. @@ -981,6 +1001,8 @@ def start_server(port): client, _ = server.accept() pydevd, proc, server_thread = _start(client, server) atexit.register(lambda: exit_handler(proc, server_thread)) + if platform.system() != 'Windows': + signal.signal(signal.SIGHUP, lambda signum, frame: signal_handler(signum, frame, proc)) return pydevd @@ -996,6 +1018,8 @@ def start_client(host, port): client.connect((host, port)) pydevd, proc, server_thread = _start(client, None) atexit.register(lambda: exit_handler(proc, server_thread)) + if platform.system() != 'Windows': + signal.signal(signal.SIGHUP, lambda signum, frame: signal_handler(signum, frame, proc)) return pydevd