From: Zack Cerza Date: Wed, 20 Dec 2023 23:19:10 +0000 (-0700) Subject: kill.kill_processes: Fix possibly-unbound variables X-Git-Tag: 1.2.0~60^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=079a88554dc5d2175b4664058f1eafe09660ce20;p=teuthology.git kill.kill_processes: Fix possibly-unbound variables Signed-off-by: Zack Cerza --- diff --git a/teuthology/kill.py b/teuthology/kill.py index 1ba4ea0ad..ec61265a5 100755 --- a/teuthology/kill.py +++ b/teuthology/kill.py @@ -180,25 +180,25 @@ def kill_processes(run_name, pids=None): else: to_kill = find_pids(run_name) - # Remove processes that don't match run-name from the set - to_check = set(to_kill) - for pid in to_check: + pids_need_sudo = set() + for pid in set(to_kill): if not process_matches_run(pid, run_name): to_kill.remove(pid) + elif psutil.Process(int(pid)).username() != getpass.getuser(): + pids_need_sudo.add(pid) + survivors = [] if len(to_kill) == 0: log.info("No teuthology processes running") else: - survivors = [] log.info("Killing Pids: " + str(to_kill)) - may_need_sudo = \ - psutil.Process(int(pid)).username() != getpass.getuser() - if may_need_sudo: + sudo_works = False + if pids_need_sudo: sudo_works = subprocess.Popen(['sudo', '-n', 'true']).wait() == 0 if not sudo_works: log.debug("Passwordless sudo not configured; not using sudo") - use_sudo = may_need_sudo and sudo_works for pid in to_kill: + use_sudo = pid in pids_need_sudo and sudo_works args = ['kill', str(pid)] # Don't attempt to use sudo if it's not necessary if use_sudo: