diff --git a/src/debugpy/adapter/sessions.py b/src/debugpy/adapter/sessions.py index a3c7bf8d..a5c523e7 100644 --- a/src/debugpy/adapter/sessions.py +++ b/src/debugpy/adapter/sessions.py @@ -7,6 +7,7 @@ from __future__ import absolute_import, division, print_function, unicode_litera import contextlib import itertools import os +import signal import threading import time @@ -260,13 +261,35 @@ class Session(util.Observable): except Exception: log.exception() - # Tell the IDE that debugging is over, but don't close the channel until it - # tells us to, via the "disconnect" request. - if self.ide and self.ide.is_connected: - try: - self.ide.channel.send_event("terminated") - except Exception: - pass + if self.ide: + if self.ide.is_connected: + # Tell the IDE that debugging is over, but don't close the channel until it + # tells us to, via the "disconnect" request. + try: + self.ide.channel.send_event("terminated") + except Exception: + pass + + if self.ide.start_request is not None and self.ide.start_request.command == "launch": + servers.stop_listening() + log.info('"launch" session ended - killing remaining debuggee processes.') + + pids_killed = set() + if self.launcher and self.launcher.pid is not None: + # Already killed above. + pids_killed.add(self.launcher.pid) + + while True: + conns = [conn for conn in servers.connections() if conn.pid not in pids_killed] + if not len(conns): + break + for conn in conns: + log.info("Killing {0}", conn) + try: + os.kill(conn.pid, signal.SIGTERM) + except Exception: + log.exception("Failed to kill {0}", conn) + pids_killed.add(conn.pid) def get(pid): diff --git a/tests/debugpy/test_multiproc.py b/tests/debugpy/test_multiproc.py index 9f075cd4..c6515116 100644 --- a/tests/debugpy/test_multiproc.py +++ b/tests/debugpy/test_multiproc.py @@ -206,7 +206,6 @@ def test_subprocess(pyfile, target, run): assert child_argv == [child, "--arg1", "--arg2", "--arg3"] -@pytest.mark.skip("https://github.com/microsoft/debugpy/issues/3") def test_autokill(pyfile, target): @pyfile def child(): @@ -244,7 +243,7 @@ def test_autokill(pyfile, target): with child_session.start(): pass - parent_session.debuggee.kill() + parent_session.request("terminate") child_session.wait_for_exit()