n-o-t-mugen opened a new pull request, #77356:
URL: https://github.com/apache/airflow/pull/77356
<!-- SPDX-License-Identifier: Apache-2.0
https://www.apache.org/licenses/LICENSE-2.0 -->
A `KubernetesPodOperator` task hangs forever when its pod has a regular
sidecar container that keeps running after the base container exits. The pod
never leaves the `Running` phase, so the task never finishes. This happens in
sync mode and on deferrable re-entry.
Builds on @yssxr's analysis and patch in #71452 (auto-closed as stale), with
their helper and test fixture fix reused and credited via `Co-authored-by`.
### Root cause
`PodManager.await_pod_completion` waits for the pod to reach a terminal
phase. It only stops early, once the base container has finished, when
`istio_enabled or do_xcom_push`. Any other long-running sidecar keeps the pod
in `Running` indefinitely. The deferrable path hits the same loop: after the
trigger fires on the base container's state, `trigger_reentry` goes through
`_clean`, which calls the same synchronous wait.
`cleanup()` has a matching gate: it decides success or failure from the pod
phase unless istio or the XCom sidecar is in use. So stopping early alone would
replace the hang with a false failure, since the phase is still `Running`.
### Fix
- **`utils/container.py`:** new `has_sidecar_containers(pod,
base_container_name)` helper (from #71452). It is true when `spec.containers`
holds any container other than the base one. Init containers, including native
sidecars (`restartPolicy: Always`), don't count, because the kubelet stops them
once the regular containers exit.
- **`utils/pod_manager.py`:** the wait loop also stops once the base
container has finished when the pod has other regular containers.
- **`operators/pod.py`:** `cleanup()` judges a pod that never reached a
terminal phase by the base container's exit code, instead of by the phase it is
stuck in.
What deliberately didn't change:
- Pods that reach `Succeeded`/`Failed` keep today's outcome logic.
- Single-container pods and pods with only native sidecars still wait for
the pod phase.
- `on_finish_action` still decides what happens to the pod: `KEEP_POD` keeps
it, `DELETE_POD` deletes it. The deletion code is untouched.
- Istio handling and the XCom sidecar flow are unchanged, and XCom is still
read before the pod is released.
No newsfragment or changelog entry, per the provider guidelines for bug
fixes.
### Tests
Unit tests (`providers/cncf/kubernetes/tests/unit/...`):
- `test_container.py`: `has_sidecar_containers`, covering no pod or spec,
base only, base plus sidecar, istio-proxy, and init containers not counting.
- `test_pod_manager.py`: `await_pod_completion` with a regular sidecar
(stops on base completion) and with a native sidecar (still waits for the pod
phase). It also fixes the `pod_factory` mock bug #71452 found, where
`MagicMock(name=...)` swallowed the container name.
- `test_pod.py`:
- `cleanup()` with a running sidecar: base succeeded with `delete_pod`,
base succeeded with `keep_pod`, and base failed. It checks the task outcome and
whether the pod is deleted.
- Deferrable `trigger_reentry` with a running sidecar: base succeeded and
base failed. It asserts there is no polling sleep and that the pod is deleted.
Kubernetes system test
(`kubernetes-tests/.../test_kubernetes_pod_operator.py`):
`test_base_container_exit_with_running_sidecar` runs a real pod. The base
container exits after 5s with code 0 or 1, while the sidecar runs `sleep
infinity`. Cases are `sync`, `deferrable`, `sync-keep-pod`, `sync-base-failed`
and `deferrable-base-failed`. Each asserts:
- the task's outcome;
- that the operator finished in under 90s;
- that the pod is either kept and still `Running` with the sidecar alive
(`KEEP_POD`), or deleted.
Results:
- **With the fix:** fork CI run
https://github.com/n-o-t-mugen/airflow/actions/runs/44607967908 has every job
green, including all K8s System jobs, the cncf.kubernetes provider tests,
static checks and mypy. The 5 new K8s sidecar cases pass in about 7s each.
- **Without the fix:** I confirmed the new tests fail with only the source
changes in `pod_manager.py` and `operators/pod.py` reverted.
- All 5 K8s sidecar cases hang to the 180s test timeout and fail.
- 5 unit tests fail: the regular-sidecar `await_pod_completion` case, the
two base-succeeded `cleanup()` cases, and both deferrable re-entry cases.
### Open questions
1. **`KEEP_POD` with a live sidecar:** the task now finishes, but the kept
pod stays `Running` until someone deletes it, because nothing stops the
sidecar. This was raised on #39694. Istio pods already behave this way today
(`kill_istio_sidecar()` exists but nothing calls it). Is that acceptable, or
should this log a warning?
2. **Behaviour change for multi-container pods:** a pod with regular
sidecars that never reaches a terminal phase is now judged by its base
container. A sidecar that misbehaves after the base container exits no longer
fails the task.
3. **Native sidecars:** pods whose only sidecars are native ones still wait
for the pod phase, relying on the kubelet to stop them. Is that the right
behaviour, or should they also stop on base completion?
closes: #39693
---
##### 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)
---
* Read the **[Pull Request
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
for more information. Note: commit author/co-author name and email in commits
become permanently public when merged.
* For fundamental code changes, an Airflow Improvement Proposal
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
is needed.
* When adding dependency, check compliance with the [ASF 3rd Party License
Policy](https://www.apache.org/legal/resolved.html#category-x).
* For significant user-facing changes create newsfragment:
`{pr_number}.significant.rst`, in
[airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
You can add this file in a follow-up commit after the PR is created so you
know the PR number.
--
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]