Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

UNS-001 [FIX] Add SIGTERM trap handlers for graceful container shutdown (#1750)

Ritwik Gfb6a3f5f36d1
diff --git a/runner/src/unstract/runner/runner.py b/runner/src/unstract/runner/runner.py
index 363ac9ed2..6b91f96ac 100644
--- a/runner/src/unstract/runner/runner.py
+++ b/runner/src/unstract/runner/runner.py
@@ -304,6 +304,13 @@ class UnstractRunner:
return tool_cmd
# Shell script components
+ # SIGTERM trap to log signal receipt but continue execution
+ sigterm_trap = (
+ "trap '"
+ 'echo "[$(date -Iseconds)] SIGTERM received in tool shell '
+ 'but ignoring - continuing" >&2'
+ "' TERM"
+ )
mkdir_cmd = f"mkdir -p {shared_log_dir}"
run_tool_fn = (
"run_tool() { "
@@ -316,8 +323,8 @@ class UnstractRunner:
)
execute_cmd = f"run_tool > {shared_log_file} 2>&1"
- # Combine all commands
- shell_script = f"{mkdir_cmd} && {run_tool_fn}; {execute_cmd}"
+ # Combine all commands with SIGTERM trap first
+ shell_script = f"{sigterm_trap}; {mkdir_cmd} && {run_tool_fn}; {execute_cmd}"
return shell_script
def _handle_tool_execution_status(
diff --git a/tool-sidecar/entrypoint.sh b/tool-sidecar/entrypoint.sh
index 558b4d2af..a31511f7e 100755
--- a/tool-sidecar/entrypoint.sh
+++ b/tool-sidecar/entrypoint.sh
@@ -1,6 +1,16 @@
#!/bin/bash
+
+# SIGTERM handler - acknowledge but don't forward to Python
+# This allows graceful shutdown by letting the log processor
+# complete naturally via sentinel-based detection
+sigterm_handler() {
+ echo "SIGTERM received in sidecar shell but ignoring - continuing" >> /shared/logs/logs.txt
+}
+trap sigterm_handler TERM
+
# Activate virtual environment
. /app/.venv/bin/activate
-# Run the application with OpenTelemetry instrumentation
-exec opentelemetry-instrument python -m unstract.tool_sidecar.log_processor
+# Run the application directly
+# Note: removed 'exec' to allow SIGTERM trap to fire
+python -m unstract.tool_sidecar.log_processor