Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_wait_procs_no_timeout(self):
sproc1 = get_test_subprocess()
sproc2 = get_test_subprocess()
sproc3 = get_test_subprocess()
procs = [psutil.Process(x.pid) for x in (sproc1, sproc2, sproc3)]
for p in procs:
p.terminate()
gone, alive = psutil.wait_procs(procs)
self.fail(fail_msg + ', and is still alive.')
worker_psutil_p = psutil.Process(loader_p.pid).children()
tester_setup_event.set()
try:
loader_p.join(JOIN_TIMEOUT + MP_STATUS_CHECK_INTERVAL)
if loader_p.is_alive():
loader_p.print_traces_of_all_threads()
fail_msg = desc + ': loader process did not terminate'
if loader_p.exception is not None:
self.fail(fail_msg + ', and had exception {}'.format(loader_p.exception))
else:
self.fail(fail_msg + ', and had no exception')
_, alive = psutil.wait_procs(worker_psutil_p, timeout=(MP_STATUS_CHECK_INTERVAL + JOIN_TIMEOUT))
if len(alive) > 0:
self.fail(desc + ': worker process (pid(s) {}) did not terminate'.format(
', '.join(str(p.pid) for p in alive)))
if exit_method is None:
self.assertEqual(loader_p.exitcode, 0)
else:
self.assertNotEqual(loader_p.exitcode, 0)
if exit_method == 'loader_error':
self.assertIsInstance(loader_p.exception, RuntimeError, desc)
self.assertIn('Loader error', str(loader_p.exception), desc)
elif exit_method == 'worker_kill':
if isinstance(loader_p.exception, RuntimeError):
self.assertIn('DataLoader worker (pid', str(loader_p.exception), desc)
elif isinstance(loader_p.exception, ConnectionRefusedError):
# Sometimes, when the worker is being killed and is freeing its
# resources, the unpickling in loader process will be met an
# psutil version > v0.4.1
procs_to_rip = main_proc.get_children(recursive=True)
except TypeError:
# psutil version <= v0.4.1
procs_to_rip = main_proc.get_children()
procs_to_rip.append(main_proc)
for proc_to_kill in procs_to_rip:
if psutil.pid_exists(proc_to_kill.pid) and proc_to_kill.is_running():
try:
proc_to_kill.terminate()
except psutil.NoSuchProcess:
continue
_, proc_still_alive = psutil.wait_procs(procs_to_rip, timeout=1)
for proc_to_atom in proc_still_alive:
if psutil.pid_exists(proc_to_atom.pid) and proc_to_atom.is_running():
try:
proc_to_atom.kill()
except psutil.NoSuchProcess:
continue
except Exception:
# Something wrong occurs with psutil
# Stop the process as usual
self._my_process.kill()
def kill (pid, including_parent = True):
try:
parent = psutil.Process(pid)
except psutil.NoSuchProcess:
return
children = parent.children(recursive=True)
for child in children:
child.kill()
psutil.wait_procs(children, timeout=5)
if including_parent:
parent.kill()
parent.wait(5)
def __wait_procs(self, procs, timeout):
before = time.time()
after = before
alive = procs
# (old versions of psutil have a bug and return too soon)
while alive and (after - before) < timeout:
next_timeout = math.ceil(timeout - (after - before))
gone, alive = psutil.wait_procs(alive, timeout=next_timeout)
after = time.time()
if after < before:
after = before
return alive
def _close_tunnel(self):
children = psutil.Process(self.process.pid).children(recursive=True)
children.insert(0, self.process)
for child in children:
child.terminate()
gone, alive = psutil.wait_procs(children, timeout=3)
for survivor in alive:
survivor.kill()
if self.tunnel_monitor_thread:
self.tunnel_monitor_thread.join(timeout=10)
"""
Safely run function: if func spawns child processes they are closed even on python error.
It can be useful when calling Stitch from Atom. For some reason RTerm.exe does not close
and Node.js isn't aware of it. So spawned Node.js process cannot exit.
"""
# noinspection PyBroadException
try:
func()
except Exception:
traceback.print_exc()
procs = psutil.Process().children(recursive=True)
for p in procs:
p.terminate()
gone, still_alive = psutil.wait_procs(procs, timeout=50)
for p in still_alive:
p.kill()
print("Killed process that was still alive after 'timeout=50' from 'terminate()' command.")
def kill_windows_process_if_running(name: str) -> None:
processes = find_procs_by_name(name)
for p in processes:
p.kill()
psutil.wait_procs(processes)
def stop(self):
# Java forks internally and detaches its children, use psutil to hunt
# them down and kill them
proc = psutil.Process(self.proc.pid)
processes = [proc] + proc.children(recursive=True)
# Be nice to begin with
for p in processes:
p.terminate()
_, alive = psutil.wait_procs(processes, timeout=3)
# But if they aren't, we can be more persuasive
for p in alive:
p.kill()
psutil.wait_procs(alive, timeout=3)
self.thread.join()
super().save_log()
def wait_for_close(self, timeout=None):
if self.process is None:
return True
(gone, alive) = psutil.wait_procs([self.process], timeout)
return alive == 0