44import typing
55from multiprocessing import Process
66
7- # process used in tests
8- process_map : dict [typing .Union [int , str ], typing .Union [Process , subprocess .Popen ]] = {}
9-
107
118def start_server (port : int , runner : typing .Callable ) -> Process :
129 p = Process (target = runner , args = (port ,))
1310 p .start ()
1411 _ping_until_success (port )
15- process_map [port ] = p
1612 return p
1713
1814
@@ -40,17 +36,19 @@ def _ping(port: int) -> bool:
4036def kill_process (process : Process ):
4137 pid = process .pid
4238 process .terminate ()
43- process .kill ()
44- _wait_for_process_to_die (process )
39+ # allow the process a moment to exit cleanly
40+ process .join (timeout = 5 )
41+ if process .is_alive ():
42+ process .kill ()
43+ _wait_for_process_to_die (process , timeout = 5 )
4544 process .join ()
46- if pid and pid in process_map :
47- del process_map [pid ]
4845
4946
50- def _wait_for_process_to_die (process , timeout : int = 5 ):
47+ def _wait_for_process_to_die (process , timeout : float = 5.0 ):
5148 start = time .time ()
5249 while time .time () - start < timeout :
5350 if not process .is_alive ():
51+ process .join ()
5452 return
5553 time .sleep (0.1 )
5654
@@ -60,7 +58,6 @@ def _wait_for_process_to_die(process, timeout: int = 5):
6058def start_subprocess (module : str ) -> subprocess .Popen :
6159 p = subprocess .Popen (["python" , "-m" , module ], shell = False ) # nosec
6260 _ping_until_success (5559 )
63- process_map [module ] = p
6461 return p
6562
6663
@@ -72,5 +69,3 @@ def kill_subprocess(process: subprocess.Popen):
7269 except subprocess .TimeoutExpired :
7370 process .kill ()
7471 process .wait ()
75- if pid and pid in process_map :
76- del process_map [pid ]
0 commit comments