kaxil commented on code in PR #77197:
URL: https://github.com/apache/airflow/pull/77197#discussion_r4724539976


##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/operators/pod.py:
##########
@@ -942,6 +956,28 @@ def execute_sync(self, context: Context):
             self.remote_pod = self.pod_manager.await_pod_completion(
                 self.pod, istio_enabled, self.base_container_name, 
self.do_xcom_push
             )
+        except AirflowTaskTimeout:
+            # The task runner calls on_kill only after this propagates, so 
cleanup has to know now
+            # that the pod must be left running for the next try to reattach. 
A retry_policy (Airflow
+            # 3.3+) can fail the task with tries left, which would leave the 
kept pod running forever.
+            ti = context["ti"]
+            if self.on_kill_action == OnKillAction.KEEP_POD:
+                if not self.reattach_on_restart:
+                    keep_declined_because = "durable execution is off"
+                elif ti.try_number > ti.max_tries:
+                    keep_declined_because = "this is the last try"
+                elif getattr(self, "retry_policy", None) is not None:
+                    keep_declined_because = "a retry_policy is set and may not 
retry"
+                else:
+                    keep_declined_because = None
+                self._keep_pod_after_timeout = keep_declined_because is None
+                if keep_declined_because:
+                    self.log.info(
+                        "on_kill_action is keep_pod, but the pod is cleaned up 
after execution_timeout "
+                        "because %s.",
+                        keep_declined_because,
+                    )
+            raise

Review Comment:
   I missed this earlier, sorry. With the pod kept, `AirflowTaskTimeout` now 
reaches the task runner's `task.on_kill()` (before, `cleanup` raised first), 
and `SparkKubernetesOperator.on_kill` ignores `on_kill_action` and always calls 
`delete_spark_job`. A Spark task with `keep_pod` logs "keeping pod ... so the 
next try can reattach" and then deletes the job, including with 
`delete_on_termination=False`, where the job used to be left alone. Could 
Spark's `on_kill` return early when `self._keep_pod_after_timeout` is set? Fine 
as a follow-up PR, it only hits Spark tasks that set `keep_pod`. 
`durable=False` doesn't opt Spark out either: its `__init__` sets 
`reattach_on_restart` back to `True` after `super().__init__()`.



-- 
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