Skip to content

Commit 028148b

Browse files
committed
Fail deferred sensors on non-timeout errors when soft_fail is set
A sensor running poke() only skips on soft_fail when it times out or asks to fail outright; every other error fails the task, so a misconfigured sensor is visible. The same sensor in deferrable mode skipped on any error, including a crashed trigger, which left users unable to tell a sensor that legitimately gave up from one that never worked. closes: apache#71255
1 parent abdc023 commit 028148b

3 files changed

Lines changed: 49 additions & 32 deletions

File tree

‎generated/known_airflow_exceptions.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,5 +418,4 @@ task-sdk/src/airflow/sdk/definitions/connection.py::4
418418
task-sdk/src/airflow/sdk/definitions/decorators/setup_teardown.py::4
419419
task-sdk/src/airflow/sdk/definitions/xcom_arg.py::1
420420
task-sdk/src/airflow/sdk/execution_time/task_runner.py::1
421-
task-sdk/tests/task_sdk/bases/test_sensor.py::1
422421
task-sdk/tests/task_sdk/execution_time/test_task_runner.py::2

‎task-sdk/src/airflow/sdk/bases/sensor.py‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@
3535
AirflowSensorTimeout,
3636
AirflowSkipException,
3737
AirflowTaskTimeout,
38-
TaskDeferralError,
3938
TaskDeferralTimeout,
4039
)
4140

4241
if TYPE_CHECKING:
4342
from airflow.sdk.definitions.context import Context
4443

44+
# soft_fail turns these into a skip whether they surface from poke() or after resuming from a
45+
# trigger; every other error fails the sensor. Both paths share the tuple so they cannot drift.
46+
_SOFT_FAIL_EXCEPTIONS = (AirflowSensorTimeout, AirflowTaskTimeout, AirflowFailException)
47+
4548

4649
class PokeReturnValue:
4750
"""
@@ -69,8 +72,8 @@ class BaseSensorOperator(BaseOperator):
6972
Sensor operators keep executing at a time interval and succeed when
7073
a criteria is met and fail if and when they time out.
7174
72-
:param soft_fail: Set to true to mark the task as SKIPPED on failure.
73-
Mutually exclusive with never_fail.
75+
:param soft_fail: Set to true to mark the task as SKIPPED when it times out or raises
76+
``AirflowFailException``. Mutually exclusive with never_fail.
7477
:param poke_interval: Time that the job should wait in between each try.
7578
Can be ``timedelta`` or ``float`` seconds.
7679
:param timeout: Time elapsed before the task times out and fails.
@@ -205,11 +208,7 @@ def run_duration() -> float:
205208
while True:
206209
try:
207210
poke_return = self.poke(context)
208-
except (
209-
AirflowSensorTimeout,
210-
AirflowTaskTimeout,
211-
AirflowFailException,
212-
) as e:
211+
except _SOFT_FAIL_EXCEPTIONS as e:
213212
if self.soft_fail:
214213
raise AirflowSkipException("Skipping due to soft_fail is set to True.") from e
215214
if self.never_fail:
@@ -251,19 +250,28 @@ def run_duration() -> float:
251250
return xcom_value
252251

253252
def resume_execution(self, next_method: str, next_kwargs: dict[str, Any] | None, context: Context):
254-
# Use nested try/except to convert TaskDeferralTimeout to AirflowSensorTimeout
255-
# while still allowing soft_fail/never_fail to handle both exception types.
253+
# Nested try/except so a trigger timeout becomes AirflowSensorTimeout before soft_fail
254+
# and never_fail are applied to it.
256255
try:
257256
try:
258257
return super().resume_execution(next_method, next_kwargs, context)
259258
except TaskDeferralTimeout as e:
260259
raise AirflowSensorTimeout(*e.args) from e
261-
except (AirflowException, TaskDeferralError) as e:
260+
except _SOFT_FAIL_EXCEPTIONS as e:
262261
if self.soft_fail:
263262
raise AirflowSkipException("Skipping due to soft_fail is set to True.") from e
264263
if self.never_fail:
265264
raise AirflowSkipException("Skipping due to never_fail is set to True.") from e
266265
raise
266+
except AirflowSkipException:
267+
raise
268+
except AirflowException as e:
269+
# execute() only skips the exceptions handled above; anything else here (a crashed
270+
# trigger, an error event raised by execute_complete) is a real failure, and soft_fail
271+
# must not hide it behind a skip.
272+
if self.never_fail:
273+
raise AirflowSkipException("Skipping due to never_fail is set to True.") from e
274+
raise
267275

268276
def _get_next_poke_interval(
269277
self,

‎task-sdk/tests/task_sdk/bases/test_sensor.py‎

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ def poke(self, context: Context):
6262

6363

6464
class DummyAsyncSensor(BaseSensorOperator):
65-
def __init__(self, return_value=False, **kwargs):
65+
def __init__(self, return_value=False, failure_exception: BaseException | None = None, **kwargs):
6666
super().__init__(**kwargs)
6767
self.return_value = return_value
68+
self.failure_exception = failure_exception or AirflowException("Sensor failed")
6869

6970
def execute_complete(self, context, event=None):
70-
raise AirflowException("Should be skipped")
71+
raise self.failure_exception
7172

7273

7374
class DummySensorWithXcomValue(BaseSensorOperator):
@@ -688,16 +689,25 @@ def test_poke_mode_only_bad_poke(self):
688689

689690

690691
class TestAsyncSensor:
692+
@pytest.mark.parametrize("soft_fail", [True, False])
693+
def test_error_from_execute_complete_fails_regardless_of_soft_fail(self, soft_fail):
694+
async_sensor = DummyAsyncSensor(task_id="dummy_async_sensor", soft_fail=soft_fail)
695+
with pytest.raises(AirflowException, match="Sensor failed"):
696+
async_sensor.resume_execution("execute_complete", None, {})
697+
691698
@pytest.mark.parametrize(
692-
("soft_fail", "expected_exception"),
699+
"failure_exception",
693700
[
694-
(True, AirflowSkipException),
695-
(False, AirflowException),
701+
pytest.param(AirflowSensorTimeout("timed out"), id="sensor-timeout"),
702+
pytest.param(AirflowTaskTimeout("timed out"), id="task-timeout"),
703+
pytest.param(AirflowFailException("failed"), id="fail-exception"),
696704
],
697705
)
698-
def test_fail_after_resuming_deferred_sensor(self, soft_fail, expected_exception):
699-
async_sensor = DummyAsyncSensor(task_id="dummy_async_sensor", soft_fail=soft_fail)
700-
with pytest.raises(expected_exception):
706+
def test_soft_fail_skips_timeout_and_fail_exceptions_after_resuming(self, failure_exception):
707+
async_sensor = DummyAsyncSensor(
708+
task_id="dummy_async_sensor", soft_fail=True, failure_exception=failure_exception
709+
)
710+
with pytest.raises(AirflowSkipException):
701711
async_sensor.resume_execution("execute_complete", None, {})
702712

703713
@pytest.mark.parametrize(
@@ -727,25 +737,25 @@ def test_timeout_after_resuming_deferred_sensor_with_never_fail(self):
727737
context={},
728738
)
729739

730-
@pytest.mark.parametrize(
731-
("soft_fail", "expected_exception"),
732-
[
733-
(True, AirflowSkipException),
734-
(False, TaskDeferralError),
735-
],
736-
)
737-
def test_trigger_failure_after_resuming_deferred_sensor_with_soft_fail(
738-
self, soft_fail, expected_exception
739-
):
740-
"""Test that deferrable sensors with soft_fail skip on trigger failure instead of failing."""
740+
@pytest.mark.parametrize("soft_fail", [True, False])
741+
def test_trigger_failure_after_resuming_deferred_sensor_fails_regardless_of_soft_fail(self, soft_fail):
741742
async_sensor = DummyAsyncSensor(task_id="dummy_async_sensor", soft_fail=soft_fail)
742-
with pytest.raises(expected_exception):
743+
with pytest.raises(TaskDeferralError):
743744
async_sensor.resume_execution(
744745
next_method="__fail__",
745746
next_kwargs={"error": TriggerFailureReason.TRIGGER_FAILURE},
746747
context={},
747748
)
748749

750+
def test_never_fail_keeps_the_skip_raised_by_execute_complete(self):
751+
async_sensor = DummyAsyncSensor(
752+
task_id="dummy_async_sensor",
753+
never_fail=True,
754+
failure_exception=AirflowSkipException("External job has skipped"),
755+
)
756+
with pytest.raises(AirflowSkipException, match="External job has skipped"):
757+
async_sensor.resume_execution("execute_complete", None, {})
758+
749759
def test_trigger_failure_after_resuming_deferred_sensor_with_never_fail(self):
750760
"""Test that deferrable sensors with never_fail skip on trigger failure."""
751761
async_sensor = DummyAsyncSensor(task_id="dummy_async_sensor", never_fail=True)

0 commit comments

Comments
 (0)