Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle SystemExit raised in the task. #36986

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions airflow/models/taskinstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2393,6 +2393,12 @@ def _run_raw_task(
self.handle_failure(e, test_mode, context, session=session)
session.commit()
raise
except SystemExit as code:
self.handle_failure(
f"Task failed due to SystemExit({code})", test_mode, context, session=session
)
session.commit()
raise
potiuk marked this conversation as resolved.
Show resolved Hide resolved
finally:
Stats.incr(f"ti.finish.{self.dag_id}.{self.task_id}.{self.state}", tags=self.stats_tags)
# Same metric with tagging
Expand Down
19 changes: 19 additions & 0 deletions tests/models/test_taskinstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2924,6 +2924,25 @@ def test_echo_env_variables(self, dag_maker):
ti.refresh_from_db()
assert ti.state == State.SUCCESS

def test_handle_system_exit(self, dag_maker):
with dag_maker():

def f(*args, **kwargs):
exit(-1)

task = PythonOperator(task_id="mytask", python_callable=f)

dr = dag_maker.create_dagrun()
ti = TI(task=task, run_id=dr.run_id)
ti.state = State.RUNNING
session = settings.Session()
session.merge(ti)
session.commit()
with pytest.raises(SystemExit):
ti._run_raw_task()
ti.refresh_from_db()
assert ti.state == State.FAILED

def test_get_current_context_works_in_template(self, dag_maker):
def user_defined_macro():
from airflow.operators.python import get_current_context
Expand Down