From ec12039f4376d3733a4b109d26db829e230d1730 Mon Sep 17 00:00:00 2001 From: Kroese Date: Tue, 28 Jul 2026 15:22:47 +0200 Subject: [PATCH] feat: Use deadline-based process timeouts (#1343) --- src/serial.sh | 9 ++++----- src/utils.sh | 19 +++++++++---------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/serial.sh b/src/serial.sh index 3eb6260..273e334 100644 --- a/src/serial.sh +++ b/src/serial.sh @@ -82,7 +82,8 @@ waitForSocket() { local socket="$1" local exit_code="$2" - local pid cnt=0 + local timeout=5 pid + local deadline=$((SECONDS + timeout)) while [ ! -S "$socket" ]; do @@ -91,14 +92,12 @@ waitForSocket() { exit "$exit_code" fi - sleep 0.1 - cnt=$((cnt + 1)) - - if (( cnt > 50 )); then + if (( SECONDS >= deadline )); then error "Failed to create qemu-host socket: $socket" exit "$exit_code" fi + sleep 0.1 done return 0 diff --git a/src/utils.sh b/src/utils.sh index eab74b0..eadf61c 100644 --- a/src/utils.sh +++ b/src/utils.sh @@ -136,14 +136,13 @@ isAlive() { waitPid() { - local i=0 local pid="$1" local timeout="${2:-10}" + local deadline=$((SECONDS + timeout)) while [ -n "$pid" ] && isAlive "$pid"; do + (( SECONDS >= deadline )) && return 1 sleep 0.2 - i=$((i + 1)) - (( i >= timeout * 5 )) && return 1 done return 0 @@ -151,16 +150,16 @@ waitPid() { waitPidFile() { - local i=0 pid + local pid local file="$1" local timeout="${2:-10}" + local deadline=$((SECONDS + timeout)) ! readPidFile pid "$file" && return 0 while [ -s "$file" ] && isAlive "$pid"; do + (( SECONDS >= deadline )) && return 1 sleep 0.2 - i=$((i + 1)) - (( i >= timeout * 5 )) && return 1 done rm -f -- "$file" @@ -183,19 +182,19 @@ pKill() { fWait() { - local i=0 local name="$1" local timeout="${2:-10}" + local deadline=$((SECONDS + timeout)) [ -z "$name" ] && return 0 while pgrep -f -l "$name" >/dev/null; do - sleep 0.2 - i=$((i + 1)) - if (( i >= timeout * 5 )); then + if (( SECONDS >= deadline )); then warn "Timed out while waiting for process: $name" break fi + + sleep 0.2 done return 0