Skip to content

Commit af4fbb8

Browse files
committed
address review (#12843): rename CLEANUP_DONE to CLEANUP_STARTED and document the re-entrancy guard
The flag is set on entry to cleanup(), not on completion: cleanup() can exit the script itself (EXIT_CLEANUP_FAILED) and that exit fires the EXIT trap, which would re-enter cleanup() while the first call is still running. The name now says so. Signed-off-by: James Peru <jmsperu@gmail.com>
1 parent f7b490b commit af4fbb8

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

scripts/vm/hypervisor/kvm/nasbackup.sh

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ BACKUP_TIMEOUT=${BACKUP_TIMEOUT:-21600}
5656
# we fail fast rather than mid-write when the NAS is near-full.
5757
MIN_FREE_SPACE=${MIN_FREE_SPACE:-1073741824}
5858

59-
# Guards cleanup() against double-execution when both an explicit call
60-
# and the EXIT trap fire (e.g. error path calls cleanup; exit 1 → trap).
61-
CLEANUP_DONE=0
59+
# Re-entrancy guard for cleanup(). It is set the moment cleanup() is ENTERED, not when it
60+
# finishes: cleanup() can exit the script itself (EXIT_CLEANUP_FAILED), and that exit fires
61+
# the EXIT trap, which would call cleanup() a second time while the first is still running.
62+
CLEANUP_STARTED=0
6263

6364
log() {
6465
[[ "$verb" -eq 1 ]] && builtin echo "$@"
@@ -515,10 +516,10 @@ check_free_space() {
515516
}
516517

517518
cleanup() {
518-
# Idempotent: skip if a prior explicit call already ran. Without this guard,
519-
# the EXIT trap would re-run cleanup and fail on the already-unmounted point.
520-
[[ $CLEANUP_DONE -eq 1 ]] && return 0
521-
CLEANUP_DONE=1
519+
# Mark "started" first (see CLEANUP_STARTED above): an explicit call followed by exit, or an
520+
# 'exit $EXIT_CLEANUP_FAILED' from inside this function, both re-enter here via the EXIT trap.
521+
[[ $CLEANUP_STARTED -eq 1 ]] && return 0
522+
CLEANUP_STARTED=1
522523

523524
local status=0
524525

0 commit comments

Comments
 (0)