import subprocess import signal import os import sys import time from multiprocessing import get_context def target(): time.sleep(99999) def test(): """ Incorrect on all versions """ proc = get_context("spawn").Process(target=target) proc.start() time.sleep(0.5) os.kill(proc.pid, signal.SIGINT) proc.join() # https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Process.exitcode # The child’s exit code. This will be None if the process has not yet # terminated. A negative value -N indicates that the child was terminated # by signal N print(f"proc.exitcode={proc.exitcode!r}") # Killed with 2, should be -2 def test_popen(): """ Incorrect on <3.7, fixed in version 3.8+ """ proc = subprocess.Popen([sys.executable, "-m", __spec__.name, "target"]) time.sleep(0.5) proc.send_signal(signal.SIGINT) print(f"proc.wait()={proc.wait()!r}") # Killed with 2, should be -2 def main(): if sys.argv[-1] == "target": return target() test_popen() test() return 0 if __name__ == "__main__": sys.exit(main())