nyxrv0id opened a new pull request, #75397:
URL: https://github.com/apache/airflow/pull/75397
### Description
In `BashOperator.get_env(context)`, when `self.append_env=False` (the
default) and `self.env` is supplied as a dictionary, `env` was assigned
directly to `self.env` without copying. When `env.update(airflow_context_vars)`
was subsequently called to export Airflow task context variables (such as
`AIRFLOW_CTX_DAG_ID`, `AIRFLOW_CTX_TASK_ID`, and `AIRFLOW_CTX_TRY_NUMBER`), it
mutated `self.env` and the caller's input dictionary in-place.
This in-place mutation causes two issues:
1. When a user defines a shared environment dictionary across multiple tasks
in a DAG file (e.g., `COMMON_ENV = {"ENV": "prod"}`), executing the first task
permanently injects that task's context variables into the shared dictionary,
leaking stale context into other tasks.
2. If an operator's `get_env` is evaluated multiple times (e.g. across
retries or test invocations), `self.env` retains stale context variables from
previous tries.
This PR fixes the mutation by making an independent shallow copy
(`self.env.copy()`) when `append_env=False`. Existing behavior for `env=None`
and `append_env=True` is preserved.
### Tests
- Added unit test `test_get_env_does_not_mutate_user_env` in
`providers/standard/tests/unit/standard/operators/test_bash.py` covering both
`append_env=False` and `append_env=True` as well as consecutive calls with
different context objects.
- Ran all existing `test_bash.py` tests locally (39 passed, 1 deselected).
- Ran `ruff check`, `ruff format --check`, and `mypy` locally with zero
errors.
---
##### Was generative AI tooling used to co-author this PR?
- [x] Yes (please specify the tool below)
Generated-by: Antigravity 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]