You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "andreahlert (via GitHub)" <gi...@apache.org> on 2026/02/08 10:14:09 UTC

[PR] Fix PythonVirtualenvOperator failing to access Variables/Connections in Airflow 3.x [airflow]

andreahlert opened a new pull request, #61629:
URL: https://github.com/apache/airflow/pull/61629

   ## What
   
   Fixes `PythonVirtualenvOperator` (and `ExternalPythonOperator`) failing to access `Variable.get()`, `Connection.get()`, and XCom via the Task SDK in Airflow 3.x environments (particularly on Kubernetes).
   
   Closes: #58724
   
   ## Why
   
   In the normal forked execution path, the supervisor communicates with the task process via a Unix socketpair mapped to fd 0 (stdin). However, the `__AIRFLOW_SUPERVISOR_FD` environment variable is never set in this path - it is only set by `InProcessTestSupervisor` when using `dag.test()`.
   
   When `PythonVirtualenvOperator` launches a subprocess via `Popen(close_fds=False)`, while fd 0 is technically inherited, the virtualenv subprocess has no reliable way to know which fd carries the supervisor comms channel. The `reinit_supervisor_comms()` function defaults to fd 0, but this is fragile and breaks when:
   1. The `airflow.sdk.execution_time.task_runner` import fails with `ImportError` (not just `ModuleNotFoundError`) due to missing transitive dependencies in the virtualenv
   2. `reinit_supervisor_comms()` itself crashes (e.g., socket validation fails), killing the entire virtualenv script
   
   Without supervisor comms, the secrets backend falls back to `EnvironmentVariablesBackend` only, silently losing access to all DB-stored Variables and Connections.
   
   ## How
   
   Two-pronged fix:
   
   ### 1. Operator side (`python.py`)
   In `_BasePythonVirtualenvOperator._execute_python_callable_in_subprocess()`, added an `elif` block that:
   - Detects the current `SUPERVISOR_COMMS` socket fd from `task_runner`
   - Marks it as inheritable via `os.set_inheritable()`
   - Propagates it to the subprocess via `__AIRFLOW_SUPERVISOR_FD` env var
   
   This makes the fd propagation explicit rather than relying on implicit fd 0 inheritance.
   
   ### 2. Template side (`python_virtualenv_script.jinja2`)
   - Broadened `except ModuleNotFoundError` to `except (ModuleNotFoundError, ImportError)` to handle cases where `task_runner` can be found but has failing transitive dependencies
   - Wrapped `reinit_supervisor_comms()` call in `try/except Exception` to prevent the entire virtualenv script from crashing if socket communication setup fails
   
   ## Testing
   
   The existing test `test_reinit_supervisor_comms` in `task-sdk/tests/task_sdk/execution_time/test_supervisor.py` validates the basic mechanism (subprocess reinits comms and fetches a connection). The fix ensures this mechanism is properly triggered in production by explicitly propagating the fd.


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix PythonVirtualenvOperator failing to access Variables/Connections in Airflow 3.x [airflow]

Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #61629:
URL: https://github.com/apache/airflow/pull/61629#discussion_r2779656776


##########
providers/standard/src/airflow/providers/standard/operators/python.py:
##########
@@ -637,6 +637,20 @@ def _execute_python_callable_in_subprocess(self, python_path: Path):
             env_vars = dict(os.environ) if self.inherit_env else {}
             if fd := os.getenv("__AIRFLOW_SUPERVISOR_FD"):
                 env_vars["__AIRFLOW_SUPERVISOR_FD"] = fd
+            elif AIRFLOW_V_3_0_PLUS and "__AIRFLOW_SUPERVISOR_FD" not in env_vars:

Review Comment:
   Connections/vars work without this change - stdin is not a normal stdin at this point, its already a socket, so the default to 0 is correct and works. 



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix PythonVirtualenvOperator failing to access Variables/Connections in Airflow 3.x [airflow]

Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #61629:
URL: https://github.com/apache/airflow/pull/61629#discussion_r2779654116


##########
providers/standard/src/airflow/providers/standard/utils/python_virtualenv_script.jinja2:
##########
@@ -55,7 +55,10 @@ else:
   #}
   reinit_supervisor_comms = getattr(task_runner, "reinit_supervisor_comms", None)
   if reinit_supervisor_comms:
-      reinit_supervisor_comms()
+      try:
+          reinit_supervisor_comms()
+      except Exception:
+          pass

Review Comment:
   This should never happen. It means your environment is broken. If you don't want this, set expect airflow to false



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix PythonVirtualenvOperator failing to access Variables/Connections in Airflow 3.x [airflow]

Posted by "andreahlert (via GitHub)" <gi...@apache.org>.
andreahlert commented on code in PR #61629:
URL: https://github.com/apache/airflow/pull/61629#discussion_r2795357379


##########
providers/standard/src/airflow/providers/standard/utils/python_virtualenv_script.jinja2:
##########
@@ -55,7 +55,10 @@ else:
   #}
   reinit_supervisor_comms = getattr(task_runner, "reinit_supervisor_comms", None)
   if reinit_supervisor_comms:
-      reinit_supervisor_comms()
+      try:
+          reinit_supervisor_comms()
+      except Exception:
+          pass

Review Comment:
   Good call, I missed that this block sits outside expect_airflow.
   
   I'll restructure it: move reinit_supervisor_comms() inside the expect_airflow block with no try/except, so a failure is loud and visible. For the not-expect_airflow path I'll keep the import attempt but just skip reinit entirely since the user explicitly opted out of depending on Airflow.
   
   Will also keep the (ModuleNotFoundError, ImportError) on the import itself since that's purely defensive for broken installs.
   
   Pushing a v2 shortly.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix PythonVirtualenvOperator failing to access Variables/Connections in Airflow 3.x [airflow]

Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on PR #61629:
URL: https://github.com/apache/airflow/pull/61629#issuecomment-3867006176

   > airflow.sdk.execution_time.task_runner import fails with ImportError (not just ModuleNotFoundError) due to missing transitive dependencies in the virtualenv
   
   What is the error you see in this case? How does it present to the user/taskligs?


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix PythonVirtualenvOperator failing to access Variables/Connections in Airflow 3.x [airflow]

Posted by "andreahlert (via GitHub)" <gi...@apache.org>.
andreahlert closed pull request #61629: Fix PythonVirtualenvOperator failing to access Variables/Connections in Airflow 3.x
URL: https://github.com/apache/airflow/pull/61629


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix PythonVirtualenvOperator failing to access Variables/Connections in Airflow 3.x [airflow]

Posted by "andreahlert (via GitHub)" <gi...@apache.org>.
andreahlert commented on code in PR #61629:
URL: https://github.com/apache/airflow/pull/61629#discussion_r2795364792


##########
providers/standard/src/airflow/providers/standard/operators/python.py:
##########
@@ -637,6 +637,20 @@ def _execute_python_callable_in_subprocess(self, python_path: Path):
             env_vars = dict(os.environ) if self.inherit_env else {}
             if fd := os.getenv("__AIRFLOW_SUPERVISOR_FD"):
                 env_vars["__AIRFLOW_SUPERVISOR_FD"] = fd
+            elif AIRFLOW_V_3_0_PLUS and "__AIRFLOW_SUPERVISOR_FD" not in env_vars:

Review Comment:
   You're right. I traced the Popen call and stdin isn't redirected, so fd 0 is inherited as-is. reinit_supervisor_comms already defaults to fd 0 when the env var is missing, so this whole block is redundant.
   
   Dropping it in v2.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix PythonVirtualenvOperator failing to access Variables/Connections in Airflow 3.x [airflow]

Posted by "andreahlert (via GitHub)" <gi...@apache.org>.
andreahlert commented on PR #61629:
URL: https://github.com/apache/airflow/pull/61629#issuecomment-3867003951

   @ashb @potiuk could you take a look at this? This fixes the supervisor comms propagation for \ in the Airflow 3.x forked execution path - the \ env var is never set, so the virtualenv subprocess can't access Variables/Connections via the Task SDK.


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix PythonVirtualenvOperator failing to access Variables/Connections in Airflow 3.x [airflow]

Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #61629:
URL: https://github.com/apache/airflow/pull/61629#discussion_r2779679307


##########
providers/standard/src/airflow/providers/standard/utils/python_virtualenv_script.jinja2:
##########
@@ -55,7 +55,10 @@ else:
   #}
   reinit_supervisor_comms = getattr(task_runner, "reinit_supervisor_comms", None)
   if reinit_supervisor_comms:
-      reinit_supervisor_comms()
+      try:
+          reinit_supervisor_comms()
+      except Exception:
+          pass

Review Comment:
   Oh this isn't in an expect airflow block. 
   
   Still, if imports at all it should be complete, else all sorts of things will break. 
   
   Is think moving this (without the exception handing) in to the expect airflow, or yet import, but only fail/log off expect airflow is yet



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix PythonVirtualenvOperator failing to access Variables/Connections in Airflow 3.x [airflow]

Posted by "andreahlert (via GitHub)" <gi...@apache.org>.
andreahlert commented on PR #61629:
URL: https://github.com/apache/airflow/pull/61629#issuecomment-3992113074

   Noting that issue #58724 was closed as fixed in 3.1.4 by PR #58621. Closing this PR since the fix is already in place.
   


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix PythonVirtualenvOperator failing to access Variables/Connections in Airflow 3.x [airflow]

Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on PR #61629:
URL: https://github.com/apache/airflow/pull/61629#issuecomment-3867920969

   > > > airflow.sdk.execution_time.task_runner import fails with ImportError (not just ModuleNotFoundError) due to missing transitive dependencies in the virtualenv
   > 
   > > 
   > 
   > > What is the error you see in this case? How does it present to the user/taskligs?
   > 
   > 
   > 
   > If the SDK is installed but some transitive dep is missing (httpx, structlog, etc), 
   
   How is that possible? Sdk lists those modules as dependencies. 
   
   
   > the import blows up with ImportError, not ModuleNotFoundError. The script just dies before the callable even runs - pretty bad UX.
   
   Does it show the error in task logs?


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix PythonVirtualenvOperator failing to access Variables/Connections in Airflow 3.x [airflow]

Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #61629:
URL: https://github.com/apache/airflow/pull/61629#discussion_r2779141943


##########
providers/standard/src/airflow/providers/standard/operators/python.py:
##########
@@ -637,6 +637,20 @@ def _execute_python_callable_in_subprocess(self, python_path: Path):
             env_vars = dict(os.environ) if self.inherit_env else {}
             if fd := os.getenv("__AIRFLOW_SUPERVISOR_FD"):
                 env_vars["__AIRFLOW_SUPERVISOR_FD"] = fd
+            elif AIRFLOW_V_3_0_PLUS and "__AIRFLOW_SUPERVISOR_FD" not in env_vars:

Review Comment:
   When is this path hit?



##########
providers/standard/src/airflow/providers/standard/utils/python_virtualenv_script.jinja2:
##########
@@ -55,7 +55,10 @@ else:
   #}
   reinit_supervisor_comms = getattr(task_runner, "reinit_supervisor_comms", None)
   if reinit_supervisor_comms:
-      reinit_supervisor_comms()
+      try:
+          reinit_supervisor_comms()
+      except Exception:
+          pass

Review Comment:
   No. If expect airflow is true then an error here should fail the task.
   
   Regardless silently ignoring the error is the worst of all paths



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix PythonVirtualenvOperator failing to access Variables/Connections in Airflow 3.x [airflow]

Posted by "andreahlert (via GitHub)" <gi...@apache.org>.
andreahlert commented on code in PR #61629:
URL: https://github.com/apache/airflow/pull/61629#discussion_r2779642226


##########
providers/standard/src/airflow/providers/standard/utils/python_virtualenv_script.jinja2:
##########
@@ -55,7 +55,10 @@ else:
   #}
   reinit_supervisor_comms = getattr(task_runner, "reinit_supervisor_comms", None)
   if reinit_supervisor_comms:
-      reinit_supervisor_comms()
+      try:
+          reinit_supervisor_comms()
+      except Exception:
+          pass

Review Comment:
   But I'd keep the try/except itself. reinit failing doesn't mean airflow isn't there, it just means the socket setup broke. 
   
   If the callable actually needs Variables/Connections it'll fail with a proper error at that point anyway. Crashing the whole task on a socket error before we even get to run user code feels wrong to me.
   
   The real fix is the fd propagation in python.py, this is just a fallback.
   
   What do ya think?



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix PythonVirtualenvOperator failing to access Variables/Connections in Airflow 3.x [airflow]

Posted by "andreahlert (via GitHub)" <gi...@apache.org>.
andreahlert commented on code in PR #61629:
URL: https://github.com/apache/airflow/pull/61629#discussion_r2779643694


##########
providers/standard/src/airflow/providers/standard/operators/python.py:
##########
@@ -637,6 +637,20 @@ def _execute_python_callable_in_subprocess(self, python_path: Path):
             env_vars = dict(os.environ) if self.inherit_env else {}
             if fd := os.getenv("__AIRFLOW_SUPERVISOR_FD"):
                 env_vars["__AIRFLOW_SUPERVISOR_FD"] = fd
+            elif AIRFLOW_V_3_0_PLUS and "__AIRFLOW_SUPERVISOR_FD" not in env_vars:

Review Comment:
   Normal forked execution. The supervisor dup2's the comms socket onto fd 0 never sets __AIRFLOW_SUPERVISOR_FD in env. So when the virtualenv operator spawns a Popen on top of that, getenv returns None and we land here.
   
   We grab the fd from the existing SUPERVISOR_COMMS and pass it through. Otherwise reinit in the child defaults to fd 0 which is stdout at that point because Popen redirected it.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix PythonVirtualenvOperator failing to access Variables/Connections in Airflow 3.x [airflow]

Posted by "andreahlert (via GitHub)" <gi...@apache.org>.
andreahlert commented on PR #61629:
URL: https://github.com/apache/airflow/pull/61629#issuecomment-3867853649

   > > airflow.sdk.execution_time.task_runner import fails with ImportError (not just ModuleNotFoundError) due to missing transitive dependencies in the virtualenv
   > 
   > What is the error you see in this case? How does it present to the user/taskligs?
   
   If the SDK is installed but some transitive dep is missing (httpx, structlog, etc), the import blows up with ImportError, not ModuleNotFoundError. The script just dies before the callable even runs - pretty bad UX.


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix PythonVirtualenvOperator failing to access Variables/Connections in Airflow 3.x [airflow]

Posted by "andreahlert (via GitHub)" <gi...@apache.org>.
andreahlert commented on code in PR #61629:
URL: https://github.com/apache/airflow/pull/61629#discussion_r2779640419


##########
providers/standard/src/airflow/providers/standard/utils/python_virtualenv_script.jinja2:
##########
@@ -55,7 +55,10 @@ else:
   #}
   reinit_supervisor_comms = getattr(task_runner, "reinit_supervisor_comms", None)
   if reinit_supervisor_comms:
-      reinit_supervisor_comms()
+      try:
+          reinit_supervisor_comms()
+      except Exception:
+          pass

Review Comment:
   Yeah you're right about the silent pass, I'll add a warning to stderr.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix PythonVirtualenvOperator failing to access Variables/Connections in Airflow 3.x [airflow]

Posted by "andreahlert (via GitHub)" <gi...@apache.org>.
andreahlert commented on PR #61629:
URL: https://github.com/apache/airflow/pull/61629#issuecomment-3886994013

   Yeah fair enough, if the SDK is pip-installed normally the transitive deps come with it. The only way it breaks is --no-deps or manual vendoring, which is an edge case I shouldn't be designing around.
   
   On the error visibility: yes it does show in task logs as a traceback, but the message isn't great since it's a raw ImportError from deep in the SDK internals. That said, that's a separate issue from what this PR should fix.
   
   Reworking the PR based on your feedback. v2 will just restructure the jinja2 template to respect expect_airflow properly and drop the python.py changes entirely.


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org