KainanP opened a new pull request, #77613: URL: https://github.com/apache/airflow/pull/77613
KubernetesExecutor issues up to three `delete_pod` calls per finished worker pod; only the first one succeeds, the others return 404. Deleting a finished pod makes Kubernetes send two more watch events for it: a MODIFIED event with `deletionTimestamp` set and then a DELETED event, both still carrying the Succeeded/Failed phase. The watcher turns each into a result, and `_change_state` calls `delete_pod` again before the `self.running` check can drop the duplicate. A running pod deleted by someone else gets the same redundant delete. The watcher now records whether the pod already has `metadata.deletionTimestamp` (`deletion_requested` on `KubernetesWatch`/`KubernetesResults`, default `False`). `_change_state` skips `delete_pod` for those results, but only in the case where it would otherwise have deleted the pod. Everything else in `_change_state` is unchanged. Why not move the `self.running` check before the pod API calls, as the issue suggests (and as earlier attempts did)? - A failed `delete_pod` (429/5xx) re-queues the result in `sync()`. If the key had already been removed from `running`, the retry would be treated as a duplicate and both the pod cleanup and the terminal event would be lost. - Pods this executor no longer tracks would stop being cleaned up, for example a pod whose create call timed out after the API server had already created it. With this change, `self.running` is untouched, so a failed delete is still retried. The re-queued result has `deletion_requested=False` and the key is still in `running`, so the retry deletes the pod and reports the state. An untracked pod that finishes is still deleted once. Tests: a regression test drives the real watcher with the MODIFIED → MODIFIED (deletionTimestamp) → DELETED sequence for a mapped task and asserts a single `delete_pod` call (3 calls on main). Further tests check that a pod already being deleted still reports its state without a delete call, and that the watcher sets the flag on the Succeeded, Failed, ProviderFailed, Pending-fatal and Running-deleted paths. Known limitations: - Duplicate status events that arrive before deletion has started can still cause one redundant delete that returns 404, as on main. That covers events with no `deletionTimestamp` yet, and an ADDED event after the watcher re-lists pods. - With `delete_option_kwargs` setting `grace_period_seconds=0`, a pod that is already Terminating because someone else deleted it is no longer force-deleted by a repeat call. It finishes with the grace period of the original deletion. closes: #72803 --- ##### Was generative AI tooling used to co-author this PR? - [X] Yes (please specify the tool below) Generated-by: Claude Code (Opus 5.5) following [the guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions) -- 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]
