diff --git a/Dockerfile b/Dockerfile index 4afb307c8f..8683aa9738 100644 --- a/Dockerfile +++ b/Dockerfile @@ -162,7 +162,7 @@ ENV GO_PATH=/usr/local/go/bin/go RUN curl --proto '=https' --tlsv1.2 -LsSf https://github.com/astral-sh/uv/releases/download/0.4.18/uv-installer.sh | sh && mv /root/.cargo/bin/uv /usr/local/bin/uv RUN curl -sL https://deb.nodesource.com/setup_20.x | bash - -RUN apt-get -y update && apt-get install -y curl nodejs awscli && apt-get clean \ +RUN apt-get -y update && apt-get install -y curl procps nodejs awscli && apt-get clean \ && rm -rf /var/lib/apt/lists/* # go build is slower the first time it is ran, so we prewarm it in the build diff --git a/backend/windmill-worker/src/bash_executor.rs b/backend/windmill-worker/src/bash_executor.rs index 128eb7d25f..ed7a1dd0f6 100644 --- a/backend/windmill-worker/src/bash_executor.rs +++ b/backend/windmill-worker/src/bash_executor.rs @@ -59,11 +59,42 @@ pub async fn handle_bash_job( append_logs(&job.id, &job.workspace_id, logs1, db).await; write_file(job_dir, "main.sh", &format!("set -e\n{content}"))?; - write_file( - job_dir, - "wrapper.sh", - &format!("set -o pipefail\nset -e\nmkfifo bp\ncat bp | tail -1 > ./result2.out &\n {bash} ./main.sh \"$@\" 2>&1 | tee bp\nwait $!", bash = BIN_BASH.as_str()), - )?; + let script = format!( + r#" +set -o pipefail +set -e + +# Function to kill child processes +cleanup() {{ + echo "Terminating child processes..." + + # Ignore SIGTERM and SIGINT + trap '' SIGTERM SIGINT + + # Kill the process group of the script (negative PID value) + pkill -P $$ + exit +}} + + +# Trap SIGTERM (or other signals) and call cleanup function +trap cleanup SIGTERM SIGINT + +# Create a named pipe +mkfifo bp + +# Start background processes +cat bp | tail -1 >> ./result2.out & + +# Run main.sh in the same process group +{bash} ./main.sh "$@" 2>&1 | tee bp & + +# Wait for all background processes to finish +wait +"#, + bash = BIN_BASH.as_str(), + ); + write_file(job_dir, "wrapper.sh", &script)?; let token = client.get_token().await; let mut reserved_variables = get_reserved_variables(job, &token, db).await?; diff --git a/docker/DockerfileSlim b/docker/DockerfileSlim index a0190933a4..16c2361a0d 100644 --- a/docker/DockerfileSlim +++ b/docker/DockerfileSlim @@ -11,7 +11,7 @@ RUN apt-get update \ && rm -rf /var/lib/apt/lists/* RUN curl -sL https://deb.nodesource.com/setup_20.x | bash - -RUN apt-get -y update && apt-get install -y curl nodejs awscli +RUN apt-get -y update && apt-get install -y curl procps nodejs awscli ENV TZ=Etc/UTC diff --git a/docker/DockerfileSlimEe b/docker/DockerfileSlimEe index ef3771c767..65c6b55a19 100644 --- a/docker/DockerfileSlimEe +++ b/docker/DockerfileSlimEe @@ -11,7 +11,7 @@ RUN apt-get update \ && rm -rf /var/lib/apt/lists/* RUN curl -sL https://deb.nodesource.com/setup_20.x | bash - -RUN apt-get -y update && apt-get install -y curl nodejs awscli +RUN apt-get -y update && apt-get install -y curl procps nodejs awscli ENV TZ=Etc/UTC