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 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
22 changes: 20 additions & 2 deletions airflow/models/taskinstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,17 @@ def _execute_task(task_instance: TaskInstance | TaskInstancePydantic, context: C
execute_callable_kwargs["next_kwargs"] = task_instance.next_kwargs
else:
execute_callable = task_to_execute.execute

def _execute_callable(context, **execute_callable_kwargs):
try:
return execute_callable(context=context, **execute_callable_kwargs)
except SystemExit as e:
# Handle only successful cases here. Failure cases will be handled upper
# in the exception chain.
if e.code is not None and e.code != 0:
raise
return None

# If a timeout is specified for the task, make it fail
# if it goes beyond
if task_to_execute.execution_timeout:
Expand All @@ -427,12 +438,12 @@ def _execute_task(task_instance: TaskInstance | TaskInstancePydantic, context: C
raise AirflowTaskTimeout()
# Run task in timeout wrapper
with timeout(timeout_seconds):
result = execute_callable(context=context, **execute_callable_kwargs)
result = _execute_callable(context=context, **execute_callable_kwargs)
except AirflowTaskTimeout:
task_to_execute.on_kill()
raise
else:
result = execute_callable(context=context, **execute_callable_kwargs)
result = _execute_callable(context=context, **execute_callable_kwargs)
with create_session() as session:
if task_to_execute.do_xcom_push:
xcom_value = result
Expand Down Expand Up @@ -2393,6 +2404,13 @@ def _run_raw_task(
self.handle_failure(e, test_mode, context, session=session)
session.commit()
raise
except SystemExit as e:
# We have already handled SystemExit with success codes (0 and None) in the `_execute_task`.
# Therefore, here we must handle only error codes.
msg = f"Task failed due to SystemExit({e.code})"
self.handle_failure(msg, test_mode, context, session=session)
session.commit()
raise Exception(msg)
finally:
Stats.incr(f"ti.finish.{self.dag_id}.{self.task_id}.{self.state}", tags=self.stats_tags)
# Same metric with tagging
Expand Down
31 changes: 31 additions & 0 deletions tests/models/test_taskinstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2924,6 +2924,37 @@ def test_echo_env_variables(self, dag_maker):
ti.refresh_from_db()
assert ti.state == State.SUCCESS

@pytest.mark.parametrize(
"code, expected_state",
[
(1, State.FAILED),
(-1, State.FAILED),
("error", State.FAILED),
(0, State.SUCCESS),
(None, State.SUCCESS),
],
)
def test_handle_system_exit(self, dag_maker, code, expected_state):
with dag_maker():

def f(*args, **kwargs):
exit(code)

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()
try:
ti._run_raw_task()
except Exception:
...
ti.refresh_from_db()
assert ti.state == expected_state

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
Loading