2020from  pdb  import  _PdbServer , _PdbClient 
2121
2222
23+ @contextmanager  
24+ def  kill_on_error (proc ):
25+     """Context manager killing the subprocess if a Python exception is raised.""" 
26+     with  proc :
27+         try :
28+             yield  proc 
29+         except :
30+             proc .kill ()
31+             raise 
32+ 
33+ 
2334class  MockSocketFile :
2435    """Mock socket file for testing _PdbServer without actual socket connections.""" 
2536
@@ -360,7 +371,7 @@ def test_connect_and_basic_commands(self):
360371        self ._create_script ()
361372        process , client_file  =  self ._connect_and_get_client_file ()
362373
363-         with  process :
374+         with  kill_on_error ( process ) :
364375            # We should receive initial data from the debugger 
365376            data  =  client_file .readline ()
366377            initial_data  =  json .loads (data .decode ())
@@ -413,7 +424,7 @@ def test_breakpoints(self):
413424        """Test setting and hitting breakpoints.""" 
414425        self ._create_script ()
415426        process , client_file  =  self ._connect_and_get_client_file ()
416-         with  process :
427+         with  kill_on_error ( process ) :
417428            # Skip initial messages until we get to the prompt 
418429            self ._read_until_prompt (client_file )
419430
@@ -451,6 +462,8 @@ def test_breakpoints(self):
451462            self .assertIn ("Function returned: 42" , stdout )
452463            self .assertEqual (process .returncode , 0 )
453464
465+     # gh-132912: The test fails randomly 
466+     @unittest .skipIf (True , "flaky test" ) 
454467    def  test_keyboard_interrupt (self ):
455468        """Test that sending keyboard interrupt breaks into pdb.""" 
456469        synchronizer_sock  =  socket .socket (socket .AF_INET , socket .SOCK_STREAM )
@@ -489,8 +502,7 @@ def bar():
489502        self ._create_script (script = script )
490503        process , client_file  =  self ._connect_and_get_client_file ()
491504
492-         with  process :
493- 
505+         with  kill_on_error (process ):
494506            # Skip initial messages until we get to the prompt 
495507            self ._read_until_prompt (client_file )
496508
@@ -520,7 +532,7 @@ def test_handle_eof(self):
520532        self ._create_script ()
521533        process , client_file  =  self ._connect_and_get_client_file ()
522534
523-         with  process :
535+         with  kill_on_error ( process ) :
524536            # Skip initial messages until we get to the prompt 
525537            self ._read_until_prompt (client_file )
526538
@@ -568,7 +580,7 @@ def run_test():
568580        self ._create_script (script = script )
569581        process , client_file  =  self ._connect_and_get_client_file ()
570582
571-         with  process :
583+         with  kill_on_error ( process ) :
572584            # First message should be an error about protocol version mismatch 
573585            data  =  client_file .readline ()
574586            message  =  json .loads (data .decode ())
@@ -591,7 +603,7 @@ def test_help_system(self):
591603        self ._create_script ()
592604        process , client_file  =  self ._connect_and_get_client_file ()
593605
594-         with  process :
606+         with  kill_on_error ( process ) :
595607            # Skip initial messages until we get to the prompt 
596608            self ._read_until_prompt (client_file )
597609
@@ -630,7 +642,7 @@ def test_multi_line_commands(self):
630642        self ._create_script ()
631643        process , client_file  =  self ._connect_and_get_client_file ()
632644
633-         with  process :
645+         with  kill_on_error ( process ) :
634646            # Skip initial messages until we get to the prompt 
635647            self ._read_until_prompt (client_file )
636648
0 commit comments