From 2d94825d17d45eef045a262e40664a61539314d2 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 24 Aug 2025 00:15:27 -0400 Subject: [PATCH] Add timeouts. --- ansible/roles/bhyve/files/bhyverc.bash | 28 ++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/ansible/roles/bhyve/files/bhyverc.bash b/ansible/roles/bhyve/files/bhyverc.bash index 0253d37..afaed5e 100644 --- a/ansible/roles/bhyve/files/bhyverc.bash +++ b/ansible/roles/bhyve/files/bhyverc.bash @@ -39,6 +39,9 @@ fi : ${VNC_HEIGHT:="1080"} : "${CD:=}" +: ${SHUTDOWN_TIMEOUT:="600"} # 10 minutes + + ############## Setup ######################### @@ -146,17 +149,38 @@ function stop_one { if ps -p "$bhyve_pid" >/dev/null; then # Send ACPI shutdown command - log "Sending ACPI shutdown to $bhyve_pid." + log "Sending ACPI shutdown to ${name}:${bhyve_pid}." kill -SIGTERM "$bhyve_pid" fi + local timeout_start timeout_end + timeout_start=$(date +%s) while ps -p "$bhyve_pid" >/dev/null; do - log "Waiting for $bhyve_pid to exit." + timeout_end=$(date +%s) + if [ $((timeout_end-timeout_start)) -ge "$SHUTDOWN_TIMEOUT" ]; then + log "${name}:${bhyve_pid} took more than $SHUTDOWN_TIMEOUT seconds to shut down. Hard powering down." + break + fi + + log "Waiting for ${name}:${bhyve_pid} to exit." sleep 2 done bhyvectl "--vm=$name" --destroy || true + local timeout_start timeout_end + timeout_start=$(date +%s) + while ps -p "$bhyve_pid" >/dev/null; do + timeout_end=$(date +%s) + if [ $((timeout_end-timeout_start)) -ge "$SHUTDOWN_TIMEOUT" ]; then + log "${name}:${bhyve_pid} took more than $SHUTDOWN_TIMEOUT seconds to hard power down. Giving up." + break + fi + + log "Waiting for ${name}:${bhyve_pid} to hard power down." + sleep 2 + done + rm -f "$pidfile" log "Finished stopping $name."