Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions airflow/models/taskinstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import operator
import os
import signal
import traceback
import warnings
from collections import defaultdict
from contextlib import nullcontext
Expand Down Expand Up @@ -3091,6 +3092,7 @@ def signal_handler(signum, frame):
os._exit(1)
return
self.log.error("Received SIGTERM. Terminating subprocesses.")
self.log.error("Stacktrace: \n%s", "".join(traceback.format_stack()))
self.task.on_kill()
raise AirflowTaskTerminated("Task received SIGTERM signal")

Expand Down
21 changes: 21 additions & 0 deletions tests/models/test_taskinstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,27 @@ def task_function(ti):
ti.run()
assert "on_failure_callback called" in caplog.text

def test_task_sigterm_calls_with_traceback_in_logs(self, dag_maker, caplog):
"""
Test that ensures that tasks print traceback to the logs when they receive sigterm
"""

def task_function(ti):
os.kill(ti.pid, signal.SIGTERM)

with dag_maker():
task_ = PythonOperator(
task_id="test_on_failure",
python_callable=task_function,
)

dr = dag_maker.create_dagrun()
ti = dr.task_instances[0]
ti.task = task_
with pytest.raises(AirflowTaskTerminated):
ti.run()
assert "Stacktrace: " in caplog.text

def test_task_sigterm_works_with_retries(self, dag_maker):
"""
Test that ensures that tasks are retried when they receive sigterm
Expand Down