]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-132912: Kill the process on error in test_remote_pdb (#132920)
authorVictor Stinner <vstinner@python.org>
Fri, 25 Apr 2025 11:14:59 +0000 (13:14 +0200)
committerGitHub <noreply@github.com>
Fri, 25 Apr 2025 11:14:59 +0000 (13:14 +0200)
If a test fails (such as an assertion error), kill the child process.

Lib/test/test_remote_pdb.py

index cc0ada12814afd79e0edf0834b46d60c508be2b6..d3267be79ece0e5bd43b0c63d7b26589d4247138 100644 (file)
@@ -20,6 +20,17 @@ import pdb
 from pdb import _PdbServer, _PdbClient
 
 
+@contextmanager
+def kill_on_error(proc):
+    """Context manager killing the subprocess if a Python exception is raised."""
+    with proc:
+        try:
+            yield proc
+        except:
+            proc.kill()
+            raise
+
+
 class MockSocketFile:
     """Mock socket file for testing _PdbServer without actual socket connections."""
 
@@ -360,7 +371,7 @@ class PdbConnectTestCase(unittest.TestCase):
         self._create_script()
         process, client_file = self._connect_and_get_client_file()
 
-        with process:
+        with kill_on_error(process):
             # We should receive initial data from the debugger
             data = client_file.readline()
             initial_data = json.loads(data.decode())
@@ -413,7 +424,7 @@ class PdbConnectTestCase(unittest.TestCase):
         """Test setting and hitting breakpoints."""
         self._create_script()
         process, client_file = self._connect_and_get_client_file()
-        with process:
+        with kill_on_error(process):
             # Skip initial messages until we get to the prompt
             self._read_until_prompt(client_file)
 
@@ -489,8 +500,7 @@ class PdbConnectTestCase(unittest.TestCase):
         self._create_script(script=script)
         process, client_file = self._connect_and_get_client_file()
 
-        with process:
-
+        with kill_on_error(process):
             # Skip initial messages until we get to the prompt
             self._read_until_prompt(client_file)
 
@@ -520,7 +530,7 @@ class PdbConnectTestCase(unittest.TestCase):
         self._create_script()
         process, client_file = self._connect_and_get_client_file()
 
-        with process:
+        with kill_on_error(process):
             # Skip initial messages until we get to the prompt
             self._read_until_prompt(client_file)
 
@@ -568,7 +578,7 @@ class PdbConnectTestCase(unittest.TestCase):
         self._create_script(script=script)
         process, client_file = self._connect_and_get_client_file()
 
-        with process:
+        with kill_on_error(process):
             # First message should be an error about protocol version mismatch
             data = client_file.readline()
             message = json.loads(data.decode())
@@ -591,7 +601,7 @@ class PdbConnectTestCase(unittest.TestCase):
         self._create_script()
         process, client_file = self._connect_and_get_client_file()
 
-        with process:
+        with kill_on_error(process):
             # Skip initial messages until we get to the prompt
             self._read_until_prompt(client_file)
 
@@ -630,7 +640,7 @@ class PdbConnectTestCase(unittest.TestCase):
         self._create_script()
         process, client_file = self._connect_and_get_client_file()
 
-        with process:
+        with kill_on_error(process):
             # Skip initial messages until we get to the prompt
             self._read_until_prompt(client_file)