@@ -424,6 +424,21 @@ def __enter__(self):
424424 self .popen .__enter__ ()
425425 return self
426426
427+ def __exit__ (self , * args , ** kwargs ):
428+ # Send SIGINT to the process group to allow it to clean up if it is running
429+ if self .popen and self .popen .poll () is None :
430+ try :
431+ os .killpg (self .popen .pid , signal .SIGINT )
432+ except Exception :
433+ pass
434+ try :
435+ # allow a graceful termination for 60 seconds
436+ self .popen .wait (timeout = 60 )
437+ except subprocess .TimeoutExpired :
438+ # If the process did not terminate, send SIGKILL
439+ os .killpg (self .popen .pid , signal .SIGKILL )
440+ self .popen .wait ()
441+
427442
428443class ParamikoBackgroundCommand (BackgroundCommand ):
429444 """
@@ -638,6 +653,21 @@ def __enter__(self):
638653 self .adb_popen .__enter__ ()
639654 return self
640655
656+ def __exit__ (self , * args , ** kwargs ):
657+ # Send SIGINT to the process group to allow it to clean up if it is running
658+ if self .adb_popen .poll () is None :
659+ try :
660+ os .killpg (self .adb_popen .pid , signal .SIGINT )
661+ except Exception :
662+ pass
663+ try :
664+ # allow a graceful termination for 60 seconds
665+ self .adb_popen .wait (timeout = 60 )
666+ except subprocess .TimeoutExpired :
667+ # If the process did not terminate, send SIGKILL
668+ os .killpg (self .adb_popen .pid , signal .SIGKILL )
669+ self .adb_popen .wait ()
670+
641671
642672class TransferManager :
643673 def __init__ (self , conn , transfer_poll_period = 30 , start_transfer_poll_delay = 30 , total_transfer_timeout = 3600 ):
0 commit comments