fix(bash): correctly propagate sigterm for cancelled bash scripts

This commit is contained in:
Ruben Fiszel
2024-10-24 14:42:37 +02:00
parent c2908e52c6
commit 2cd46cc55f
4 changed files with 39 additions and 8 deletions
+1 -1
View File
@@ -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
+36 -5
View File
@@ -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?;
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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