I have a piece of python code that should spawn an interruptible task in a child process: class Script: ''' Class instantiated by the parent process ''' def __init__(self, *args, **kwargs): self.process = None self.start() def __del__(self): if self.process: if self.process.poll() is None: self.stop() def start(self): popen_kwargs = { 'executable': sys.executable, 'creationflags': 0 * subprocess.CREATE_DEFAULT_ERROR_MODE | subprocess.CREATE_NEW_PROCESS_GROUP, } self.process = subprocess.Popen(['python', os.path.realpath(__file__)], **popen_kwargs) def stop(self): if not self.process: return try: self.process.send_signal(signal.CTRL_C_EVENT) self.process.wait() self.process = None except KeyboardInterrupt: pass class ScriptSubprocess: def __init__(self): self.stop = False def run(self): try: while not self.stop: # ... except KeyboardInterrupt: # interrupted! pass finally: # make a *clean* exit if interrupted self.stop = True if __name__ == '__main__': p = ScriptSubprocess() p.run() del p and it works fine in a standalone python interpreter. The problem arises when I move this code in the real application, which has an embedded Python interpreter. In this case, it hangs when trying to stop the child process at the line self.process.wait(), indicating the previous line self.process.send_signal(signal.CTRL_C_EVENT) did not work and in fact the child process is still running and if I manually terminate it via task manager, the call to self.process.wait() returns as if it has succeeded stopping the child process. I am looking for possible causes (e.g. some process flag of the parent process) that disables CTRL_C_EVENT. The documentation of subprocess says: Popen.send_signal(signal) Sends the signal signal to the child. Do nothing if the process completed. Note On Windows, SIGTERM is an alias for terminate(). CTRL_C_EVENT and CTRL_BREAK_EVENT can be sent to processes started with a creationflags parameter which includes CREATE_NEW_PROCESS_GROUP. and also: subprocess.CREATE_NEW_PROCESS_GROUP A Popen creationflags parameter to specify that a new process group will be created. This flag is necessary for using os.kill() on the subprocess. This flag is ignored if CREATE_NEW_CONSOLE is specified. So I am using creationflags with subprocess.CREATE_NEW_PROCESS_GROUP, but still it is unable to kill the subprocess with CTRL_C_EVENT in the real application. (same as without this flag) Since the real application (i.e. the parent process) is also using SetConsoleCtrlHandler to handle certain signals, I also try to pass creationflags with subprocess.CREATE_DEFAULT_ERROR_MODE to override that error mode in the child process, but still unable to kill the child process with CTRL_C_EVENT. Note: CTRL_BREAK_EVENT works but does not give a clean exit (i.e. the finally: clause is not executed). My guess is that SetConsoleCtrlHandler is the culprit, but I have no means of avoiding that being called in the parent process, or undoing its effect... Continue reading...