From 7d04ad75321ee6df9f7284ce984f3bf6fa6b08df Mon Sep 17 00:00:00 2001 From: Wang Defa Date: Fri, 26 Dec 2025 15:14:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BD=BB=E5=BA=95=E4=BF=AE=E5=A4=8D=20F?= =?UTF-8?q?IFO=20=E6=B8=85=E7=90=86=E9=97=AE=E9=A2=98=EF=BC=88=E9=81=BF?= =?UTF-8?q?=E5=85=8D=20trap=20=E5=86=B2=E7=AA=81=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 问题根源 存在**两个 EXIT trap 冲突**: 1. `oci/create_instance.sh:364` - 清理 FIFO 的 trap 2. `common/remote_loader.sh:23` - 清理临时目录的 trap 在 Bash 中,每次设置新的 trap 会**覆盖**之前的 trap。 当 remote_loader.sh 加载时,它的 `trap cleanup_remote_libs EXIT` 会覆盖 main 函数中设置的 FIFO 清理 trap。 ## 错误表现 ``` /dev/fd/63: line 1: fifo: unbound variable ``` - FIFO 文件不会被清理(因为 trap 被覆盖) - Process substitution 退出时产生 trap 交互错误 ## 修复方案 **移除 trap,改用显式清理:** ```bash # 旧方案(有问题) trap "rm -f \"$fifo\"" EXIT configure_network "$ocid" "$fifo" & read subnet_id < "$fifo" # 新方案(可靠) configure_network "$ocid" "$fifo" & local bg_pid=$! # 保存后台进程 PID read subnet_id < "$fifo" # 读取结果 wait "$bg_pid" # 等待后台进程完成 rm -f "$fifo" # 手动清理 FIFO ``` ## 优势 - ✅ 避免 trap 冲突 - ✅ 显式的清理流程,更易理解 - ✅ 确保后台任务完成后再清理 - ✅ 与 remote_loader.sh 的 trap 兼容 --- oci/create_instance.sh | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/oci/create_instance.sh b/oci/create_instance.sh index ddd6c2d..b8e0462 100644 --- a/oci/create_instance.sh +++ b/oci/create_instance.sh @@ -361,9 +361,18 @@ main() { local fifo fifo=$(mktemp -u) mkfifo "$fifo" - trap "rm -f \"$fifo\"" EXIT + + # 启动后台任务 configure_network "$ocid" "$fifo" & + local bg_pid=$! + + # 读取结果 read subnet_id < "$fifo" + + # 等待后台任务完成并清理 + wait "$bg_pid" + rm -f "$fifo" + log_success "网络配置完成 (子网ID: $subnet_id)" else log_info "使用指定的子网ID: $subnet_id"