Skip to content

_airflow_parsing_context_manager writes to os.environ after program has started #37484

@hterik

Description

@hterik

Apache Airflow version

2.8.1

If "Other Airflow 2 version" selected, which one?

No response

What happened?

After observing _airflow_parsing_context_manager in airflow/utils/dag_parsing_context.py i can see that it is modifying the os.environ when entering the context manager.
Writing to os.environ is generally a bad idea, because lower level getenv/setenv functions are not thread safe. Meaning that, unless you have 100% control over that no other threads have been spawned yet, the only way to ensure thread safety is to never write to environment [1]
In the context where this code is executing, i believe this is very hard to guarantee.

From what i can tell, these envvars are never used outside of this python module, so i believe there is no need for these to be environment variables. Unless there is an expectation of it being passed through several airflow invocations.
If that is the case, the solution in this case is simple, instead of using environment variables, one can use Pythons contextlib.ContextVar.
Plain global python variables here would also work equally good.

[1] https://news.ycombinator.com/item?id=37908655

What you think should happen instead?

No response

How to reproduce

See above

Operating System

NA

Versions of Apache Airflow Providers

No response

Deployment

Other

Deployment details

No response

Anything else?

No response

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions