@@ -409,6 +409,10 @@ class AutoInterrupt(object):
409409
410410__slots__ = ("proc" ,"args" ,"status" )
411411
412+ # If this is non-zero it will override any status code during
413+ # _terminate, used to prevent race conditions in testing
414+ _status_code_if_terminate :int = 0
415+
412416def __init__ (self ,proc :Union [None ,subprocess .Popen ],args :Any )-> None :
413417self .proc = proc
414418self .args = args
@@ -427,11 +431,10 @@ def _terminate(self) -> None:
427431proc .stdout .close ()
428432if proc .stderr :
429433proc .stderr .close ()
430-
431434# did the process finish already so we have a return code ?
432435try :
433436if proc .poll ()is not None :
434- self .status = proc .poll ()
437+ self .status = self . _status_code_if_terminate or proc .poll ()
435438return None
436439except OSError as ex :
437440log .info ("Ignored error after process had died: %r" ,ex )
@@ -443,7 +446,9 @@ def _terminate(self) -> None:
443446# try to kill it
444447try :
445448proc .terminate ()
446- self .status = proc .wait ()# ensure process goes away
449+ status = proc .wait ()# ensure process goes away
450+
451+ self .status = self ._status_code_if_terminate or status
447452except OSError as ex :
448453log .info ("Ignored error after process had died: %r" ,ex )
449454except AttributeError :
@@ -849,7 +854,7 @@ def execute(self,
849854
850855if is_win :
851856cmd_not_found_exception = OSError
852- if kill_after_timeout :
857+ if kill_after_timeout is not None :
853858raise GitCommandError (redacted_command ,'"kill_after_timeout" feature is not supported on Windows.' )
854859else :
855860cmd_not_found_exception = FileNotFoundError # NOQA # exists, flake8 unknown @UndefinedVariable
@@ -916,7 +921,7 @@ def _kill_process(pid: int) -> None:
916921return
917922# end
918923
919- if kill_after_timeout :
924+ if kill_after_timeout is not None :
920925kill_check = threading .Event ()
921926watchdog = threading .Timer (kill_after_timeout ,_kill_process ,args = (proc .pid ,))
922927
@@ -927,10 +932,10 @@ def _kill_process(pid: int) -> None:
927932newline = "\n " if universal_newlines else b"\n "
928933try :
929934if output_stream is None :
930- if kill_after_timeout :
935+ if kill_after_timeout is not None :
931936watchdog .start ()
932937stdout_value ,stderr_value = proc .communicate ()
933- if kill_after_timeout :
938+ if kill_after_timeout is not None :
934939watchdog .cancel ()
935940if kill_check .is_set ():
936941stderr_value = ('Timeout: the command "%s" did not complete in %d '