Skip to content

Commit

Permalink
Handle task timeout to send SIGTERM
Browse files Browse the repository at this point in the history
Without this, Python subprocess sends SIGKILL on timeout, leaving no
chance of graceful shutdown.
  • Loading branch information
Adri2000 committed Aug 10, 2023
1 parent 31037cc commit 3800f35
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions rebootmgr/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,15 @@ def run_tasks(tasktype, con, hostname, dryrun, task_timeout):
for task in sorted(os.listdir("/etc/rebootmgr/%s_tasks/" % tasktype)):
task = os.path.join("/etc/rebootmgr/%s_tasks" % tasktype, task)
LOG.info("Run task %s" % task)
p = subprocess.Popen(task, env=env)
try:
subprocess.run(task, check=True, env=env, timeout=(task_timeout * 60))
except subprocess.CalledProcessError as e:
LOG.error("Task %s failed with return code %s. Exit" % (task, e.returncode))
sys.exit(EXIT_TASK_FAILED)
ret = p.wait(timeout=(task_timeout * 60))
except subprocess.TimeoutExpired:
p.terminate()
try:
p.wait(timeout=10)
except subprocess.TimeoutExpired:
p.kill()
LOG.error("Could not finish task %s in %i minutes. Exit" % (task, task_timeout))
LOG.error("Disable rebootmgr in consul for this node")
data = get_config(con, hostname)
Expand All @@ -84,6 +87,9 @@ def run_tasks(tasktype, con, hostname, dryrun, task_timeout):
put_config(con, hostname, data)
con.kv.delete("service/rebootmgr/reboot_in_progress")
sys.exit(EXIT_TASK_FAILED)
if ret != 0:
LOG.error("Task %s failed with return code %s. Exit" % (task, ret))
sys.exit(EXIT_TASK_FAILED)
LOG.info("task %s finished" % task)


Expand Down

0 comments on commit 3800f35

Please sign in to comment.