https://github.com/python/cpython/commit/79ba56433e5ced7740866d1112b0cead86f627f6
commit: 79ba56433e5ced7740866d1112b0cead86f627f6
branch: main
author: Victor Stinner <vstin...@python.org>
committer: vstinner <vstin...@python.org>
date: 2025-04-25T13:14:59+02:00
summary:

gh-132912: Kill the process on error in test_remote_pdb (#132920)

If a test fails (such as an assertion error), kill the child process.

files:
M Lib/test/test_remote_pdb.py

diff --git a/Lib/test/test_remote_pdb.py b/Lib/test/test_remote_pdb.py
index cc0ada12814afd..d3267be79ece0e 100644
--- a/Lib/test/test_remote_pdb.py
+++ b/Lib/test/test_remote_pdb.py
@@ -20,6 +20,17 @@
 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 @@ def test_connect_and_basic_commands(self):
         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 @@ def test_breakpoints(self):
         """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 @@ def bar():
         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 @@ def test_handle_eof(self):
         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 @@ def run_test():
         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 @@ def test_help_system(self):
         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 @@ def test_multi_line_commands(self):
         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)
 

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to