Skip to content

Fix race condition in AWS CLI cache creation during parallel KubernetesPodOperator auth (#60943) - #61935

Closed
Vamsi-klu wants to merge 1 commit into
apache:mainfrom
Vamsi-klu:fix/aws-cli-cache-race-condition-60943
Closed

Vamsi-klu wants to merge 1 commit into
apache:mainfrom
Vamsi-klu:fix/aws-cli-cache-race-condition-60943

Conversation

@Vamsi-klu

@Vamsi-klu Vamsi-klu commented Feb 15, 2026 •

Copy link
Copy Markdown

Summary

  • Pre-create ~/.aws/cli/cache directory in KubernetesHook.get_conn() to prevent a FileExistsError race condition when multiple KPO tasks authenticate via aws eks get-token concurrently on the same Celery worker
  • Older botocore versions (<1.40.2) call os.makedirs() without exist_ok=True, causing intermittent task failures before pod creation

Root Cause

When parallel KubernetesPodOperator tasks invoke exec-based EKS authentication on the same worker, the AWS CLI races to create ~/.aws/cli/cache. The losing process gets FileExistsError (errno 17), which surfaces as a 403 Forbidden from the Kubernetes API — the task fails before the pod is even created.

Fixed upstream in botocore 1.40.2, but this defensive fix protects users on older versions.

Why this approach (and not something else)

We considered several alternatives before landing on defensive directory pre-creation:

Approach Why we rejected it
Retry on 403 in generic_api_retry 403 is normally a permanent permissions error. Adding it to TRANSIENT_STATUS_CODES would mask real auth failures and add retry latency to every legitimate 403. Distinguishing transient exec-auth 403s from real permission denials is not reliably possible — the Kubernetes client's ExecProvider silently swallows the subprocess error and proceeds with a bad token, so the 403 looks identical to a genuine RBAC denial.
threading.Lock around config loading The exec plugin (aws eks get-token) runs lazily during the first API call, not during config.load_kube_config(). A lock around config loading wouldn't prevent the race. Locking around every API call would serialize all K8s operations — unacceptable for performance.
Parse kubeconfig to detect exec-based auth Over-engineered for a one-line fix. Would add complexity, fragile YAML parsing, and still need per-tool knowledge of which cache dirs to create.
Pin botocore >= 1.40.2 as a dependency The Kubernetes provider has no direct dependency on botocore and shouldn't. AWS is just one of many possible exec-based auth backends.
Documentation-only (recommend botocore upgrade) Doesn't help users who can't control their botocore version (e.g., managed Airflow platforms like Astronomer).

Why pre-creation wins:

  • It's a single os.makedirs(..., exist_ok=True) call — the exact same fix botocore 1.40.2 applied, just done earlier in the call chain
  • exist_ok=True is inherently safe for concurrent invocations — no race between our pre-creation and the AWS CLI
  • Zero performance overhead (one syscall, idempotent)
  • Zero risk of masking real errors — we don't change retry behavior or error handling
  • Protects all users regardless of their botocore version

Changes

  • hooks/kubernetes.py: Added _ensure_exec_plugin_cache_dirs() function called from get_conn() before any kube config loading. Uses os.makedirs(..., exist_ok=True) to pre-create the cache directory.
  • test_kubernetes.py: 3 new test cases verifying directory creation, idempotency, and integration with get_conn().

closes: #60943

Test plan

  • New unit tests verify directory creation, idempotency, and integration
  • Manual: Run parallel KPO tasks on same Celery worker with EKS auth and botocore < 1.40.2

Note to users: Upgrading to botocore >= 1.40.2 also resolves this at the source. This fix provides a safety net for environments that cannot upgrade immediately.

…esPodOperator auth (apache#60943)

Pre-create ~/.aws/cli/cache directory in KubernetesHook.get_conn() to
prevent a FileExistsError race condition when multiple KPO tasks
authenticate via `aws eks get-token` concurrently on the same Celery
worker.

Older botocore versions (<1.40.2) call os.makedirs() without
exist_ok=True, causing intermittent task failures before pod creation.

closes: apache#60943
# authentication (e.g. ``aws eks get-token``). Pre-creating it avoids the race.
# See: https://github.com/apache/airflow/issues/60943
# https://github.com/boto/botocore/commit/f1c1bc90
_AWS_CLI_CACHE_DIR = os.path.join(os.path.expanduser("~"), ".aws", "cli", "cache")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar like my comments in #61936: Why are there AWS specifc changes made in K8s standard package? If AWS specific they should land in AWS provider.

This comment was marked as spam.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@o-nikolas / @eladkal same here - any guidance from your experience?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same answer as here

I think I prefer 1 or 3, I don't like the complication of 2 for such a small usecase

This comment was marked as spam.

@Vamsi-klu

This comment was marked as spam.

@Vamsi-klu

This comment was marked as spam.

@jscheffl

Copy link
Copy Markdown
Contributor

I do not see any changes, is there a new PR? Or forgot to push?

Can same approach also be made for #61936 (comment) ?

@potiuk

potiuk commented Mar 12, 2026 •

Copy link
Copy Markdown
Member

@Vamsi-klu This PR has been converted to draft because it does not yet meet our Pull Request quality criteria.

Issues found:

  • ❌ Pre-commit / static checks: Failing: CI image checks / Static checks. Run prek run --from-ref main locally to find and fix issues. See Pre-commit / static checks docs.
  • ⚠️ Unresolved review comments: This PR has 1 unresolved review thread from maintainers. Please review and resolve all inline review comments before requesting another review. You can resolve a conversation by clicking 'Resolve conversation' on each thread after addressing the feedback. See pull request guidelines.

Note: Your branch is 734 commits behind main. Some check failures may be caused by changes in the base branch rather than by your PR. Please rebase your branch and push again to get up-to-date CI results.

What to do next:

  • The comment informs you what you need to do.
  • Fix each issue, then mark the PR as "Ready for review" in the GitHub UI - but only after making sure that all the issues are fixed.
  • Maintainers will then proceed with a normal review.

Converting a PR to draft is not a rejection — it is an invitation to bring the PR up to the project's standards so that maintainer review time is spent productively. If you have questions, feel free to ask on the Airflow Slack.


Note: This comment was drafted by an AI-assisted triage tool and may contain mistakes. Once you have addressed the points above, an Apache Airflow maintainer — a real person — will take the next look at your PR. We use this two-stage triage process so that our maintainers' limited time is spent where it matters most: the conversation with you.

@Vamsi-klu

This comment was marked as spam.

@eladkal

eladkal commented Mar 22, 2026

Copy link
Copy Markdown
Contributor

closing in favor of #63610

@eladkal eladkal closed this Mar 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers provider:cncf-kubernetes Kubernetes (k8s) provider related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Race Condition in AWS CLI Cache Creation During Parallel KubernetesPodOperator Authentication

5 participants