amoghrajesh commented on code in PR #45310:
URL: https://github.com/apache/airflow/pull/45310#discussion_r1899977240


##########
task_sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -390,19 +390,37 @@ def run(ti: RuntimeTaskInstance, log: Logger):
         #   - Update RTIF
         #   - Pre Execute
         #   etc
-        result = ti.task.execute(context)  # type: ignore[attr-defined]
-        _push_xcom_if_needed(result, ti)
 
+        result = None
+        if ti.task.execution_timeout:
+            # TODO: handle timeout in case of deferral
+            from airflow.utils.timeout import timeout
+
+            timeout_seconds = ti.task.execution_timeout.total_seconds()
+            try:
+                # It's possible we're already timed out, so fast-fail if true
+                if timeout_seconds <= 0:
+                    raise AirflowTaskTimeout()
+                # Run task in timeout wrapper
+                with timeout(timeout_seconds):
+                    result = ti.task.execute(context)  # type: 
ignore[attr-defined]
+            except AirflowTaskTimeout:
+                # TODO: handle on kill callback here

Review Comment:
   Will be done when we port call backs



##########
task_sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -390,19 +390,37 @@ def run(ti: RuntimeTaskInstance, log: Logger):
         #   - Update RTIF
         #   - Pre Execute
         #   etc
-        result = ti.task.execute(context)  # type: ignore[attr-defined]
-        _push_xcom_if_needed(result, ti)
 
+        result = None
+        if ti.task.execution_timeout:
+            # TODO: handle timeout in case of deferral

Review Comment:
   Will handle this in a follow up, getting basic functionality in now



##########
task_sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -425,7 +443,14 @@ def run(ti: RuntimeTaskInstance, log: Logger):
         )
 
         # TODO: Run task failure callbacks here
-    except (AirflowTaskTimeout, AirflowException):
+    except AirflowTaskTimeout:
+        # We should allow retries if the task has defined it
+        msg = TaskState(
+            state=TerminalTIState.FAILED,
+            end_date=datetime.now(tz=timezone.utc),
+        )
+        # TODO: Run task failure callbacks here

Review Comment:
   Might conflict with https://github.com/apache/airflow/pull/45308. Will 
handle it once the first of these 2 is merged



##########
task_sdk/tests/execution_time/test_task_runner.py:
##########
@@ -330,6 +330,48 @@ def test_run_raises_system_exit(time_machine, 
mocked_parse, make_ti_context, moc
     )
 
 
+def test_run_task_timeout(time_machine, mocked_parse, make_ti_context, 
mock_supervisor_comms):
+    """Test running a basic task that times out."""
+    from time import sleep
+
+    from airflow.providers.standard.operators.python import PythonOperator
+
+    task = PythonOperator(
+        task_id="sleep",
+        execution_timeout=timedelta(milliseconds=10),
+        python_callable=lambda: sleep(2),

Review Comment:
   Keeping a minimum "kill" delay to not add overhead to the test infra



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to