From 5f05663706d7eda7856c76e061bddc49368710d8 Mon Sep 17 00:00:00 2001 From: nearxos Date: Mon, 23 Feb 2026 10:32:07 +0200 Subject: [PATCH] Implement graceful cancellation for cloud-init image compression Add a cleanup function to handle cancellation of the xz compression process in the build-cloudinit-image.sh script. This enhancement allows for a more robust response to cancellation requests, ensuring that resources are properly released and status messages are updated accordingly. The script now traps termination signals and cleans up temporary files, improving the overall reliability of the cloud-init image building workflow. --- .../host/build-cloudinit-image.sh | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/emmc-provisioning/host/build-cloudinit-image.sh b/emmc-provisioning/host/build-cloudinit-image.sh index 414999e..0338ca1 100644 --- a/emmc-provisioning/host/build-cloudinit-image.sh +++ b/emmc-provisioning/host/build-cloudinit-image.sh @@ -193,21 +193,31 @@ cp "$IMG_FILE" "$OUT_PATH" check_cancel write_status "finalizing" "Compressing image (xz)…" "" "" OUT_XZ="${OUT_PATH}.xz" +XZ_PID="" +cleanup_xz_and_exit() { + local phase="${1:-cancelled}" + if [[ -n "$XZ_PID" ]] && kill -0 "$XZ_PID" 2>/dev/null; then + kill "$XZ_PID" 2>/dev/null + wait "$XZ_PID" 2>/dev/null || true + fi + write_status "$phase" "Build cancelled." "" "" + rm -f "$REQUEST_FILE" "$CANCEL_FILE" + exit 0 +} +trap 'cleanup_xz_and_exit "cancelled"' TERM INT xz -T 0 -z -k -f "$OUT_PATH" 2>/dev/null & XZ_PID=$! while kill -0 "$XZ_PID" 2>/dev/null; do if [[ -f "$CANCEL_FILE" ]]; then - kill "$XZ_PID" 2>/dev/null - wait "$XZ_PID" 2>/dev/null - write_status "cancelled" "Build cancelled." "" "" - rm -f "$REQUEST_FILE" "$CANCEL_FILE" - exit 0 + cleanup_xz_and_exit "cancelled" fi sleep 2 done wait "$XZ_PID" 2>/dev/null || true +trap - TERM INT if [[ ! -f "$OUT_XZ" ]]; then xz -T 1 -z -k -f "$OUT_PATH" 2>/dev/null || true + fi if [[ -f "$OUT_XZ" ]]; then rm -f "$OUT_PATH"