1515from git .odict import OrderedDict
1616from contextlib import contextmanager
1717import signal
18+ import subprocess
1819from subprocess import (
1920 call ,
2021 Popen ,
@@ -229,6 +230,15 @@ def dict_to_slots_and__excluded_are_none(self, d, excluded=()):
229230
230231## -- End Utilities -- @}
231232
233+ # value of Windows process creation flag taken from MSDN
234+ CREATE_NO_WINDOW = 0x08000000
235+
236+ ## CREATE_NEW_PROCESS_GROUP is needed to allow killing it afterwards,
237+ # seehttps://docs.python.org/3/library/subprocess.html#subprocess.Popen.send_signal
238+ PROC_CREATIONFLAGS = (CREATE_NO_WINDOW | subprocess .CREATE_NEW_PROCESS_GROUP
239+ if sys .platform == 'win32'
240+ else 0 )
241+
232242
233243class Git (LazyMixin ):
234244
@@ -267,9 +277,6 @@ def __setstate__(self, d):
267277 # Enables debugging of GitPython's git commands
268278 GIT_PYTHON_TRACE = os .environ .get ("GIT_PYTHON_TRACE" , False )
269279
270- # value of Windows process creation flag taken from MSDN
271- CREATE_NO_WINDOW = 0x08000000
272-
273280 # Provide the full path to the git executable. Otherwise it assumes git is in the path
274281 _git_exec_env_var = "GIT_PYTHON_GIT_EXECUTABLE"
275282 GIT_PYTHON_GIT_EXECUTABLE = os .environ .get (_git_exec_env_var , git_exec_name )
@@ -317,7 +324,7 @@ def __del__(self):
317324
318325 # try to kill it
319326 try :
320- os . kill ( proc .pid , 2 ) # interrupt signal
327+ proc .terminate ()
321328 proc .wait () # ensure process goes away
322329 except (OSError , WindowsError ):
323330 pass # ignore error when process already died
@@ -632,7 +639,6 @@ def execute(self, command,
632639 cmd_not_found_exception = OSError
633640 # end handle
634641
635- creationflags = self .CREATE_NO_WINDOW if sys .platform == 'win32' else 0
636642 try :
637643 proc = Popen (command ,
638644 env = env ,
@@ -644,7 +650,7 @@ def execute(self, command,
644650 shell = self .USE_SHELL ,
645651 close_fds = (os .name == 'posix' ), # unsupported on windows
646652 universal_newlines = universal_newlines ,
647- creationflags = creationflags ,
653+ creationflags = PROC_CREATIONFLAGS ,
648654 ** subprocess_kwargs
649655 )
650656 except cmd_not_found_exception as err :
@@ -655,7 +661,8 @@ def execute(self, command,
655661
656662 def _kill_process (pid ):
657663 """ Callback method to kill a process. """
658- p = Popen (['ps' , '--ppid' , str (pid )], stdout = PIPE , creationflags = creationflags )
664+ p = Popen (['ps' , '--ppid' , str (pid )], stdout = PIPE ,
665+ creationflags = PROC_CREATIONFLAGS )
659666 child_pids = []
660667 for line in p .stdout :
661668 if len (line .split ()) > 0 :
0 commit comments