You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by ro...@apache.org on 2024/08/04 07:50:36 UTC

(airflow) branch main updated: Clean up the exception handler when run_as_user is the airflow user (#41241)

This is an automated email from the ASF dual-hosted git repository.

rom pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 36efcff72b Clean up the exception handler when run_as_user is the airflow user  (#41241)
36efcff72b is described below

commit 36efcff72be07d5caa1d0e21cbee939123bded08
Author: rom sharon <33...@users.noreply.github.com>
AuthorDate: Sun Aug 4 10:50:29 2024 +0300

    Clean up the exception handler when run_as_user is the airflow user  (#41241)
---
 airflow/jobs/local_task_job_runner.py           | 8 ++++++--
 tests/task/task_runner/test_base_task_runner.py | 8 ++++----
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/airflow/jobs/local_task_job_runner.py b/airflow/jobs/local_task_job_runner.py
index a6a1f0ac8f..48eb547a19 100644
--- a/airflow/jobs/local_task_job_runner.py
+++ b/airflow/jobs/local_task_job_runner.py
@@ -33,7 +33,7 @@ from airflow.utils import timezone
 from airflow.utils.log.file_task_handler import _set_task_deferred_context_var
 from airflow.utils.log.logging_mixin import LoggingMixin
 from airflow.utils.net import get_hostname
-from airflow.utils.platform import IS_WINDOWS
+from airflow.utils.platform import IS_WINDOWS, getuser
 from airflow.utils.session import NEW_SESSION, provide_session
 from airflow.utils.state import TaskInstanceState
 
@@ -295,7 +295,11 @@ class LocalTaskJobRunner(BaseJobRunner, LoggingMixin):
             recorded_pid = ti.pid
             same_process = recorded_pid == current_pid
 
-            if recorded_pid is not None and (ti.run_as_user or self.task_runner.run_as_user):
+            is_child_process = (ti.run_as_user and (ti.run_as_user != getuser())) or (
+                self.task_runner.run_as_user and (self.task_runner != getuser())
+            )
+
+            if recorded_pid is not None and is_child_process:
                 # when running as another user, compare the task runner pid to the parent of
                 # the recorded pid because user delegation becomes an extra process level.
                 # However, if recorded_pid is None, pass that through as it signals the task
diff --git a/tests/task/task_runner/test_base_task_runner.py b/tests/task/task_runner/test_base_task_runner.py
index 7bcf0a9191..7a713f0ded 100644
--- a/tests/task/task_runner/test_base_task_runner.py
+++ b/tests/task/task_runner/test_base_task_runner.py
@@ -29,7 +29,7 @@ from airflow.task.task_runner.base_task_runner import BaseTaskRunner
 pytestmark = pytest.mark.db_test
 
 
-@pytest.mark.parametrize(["impersonation"], (("nobody",), (None,)))
+@pytest.mark.parametrize(["impersonation"], (("nobody",), ("airflow",), (None,)))
 @mock.patch("subprocess.check_call")
 @mock.patch("airflow.task.task_runner.base_task_runner.tmp_configuration_copy")
 def test_config_copy_mode(tmp_configuration_copy, subprocess_call, dag_maker, impersonation):
@@ -51,9 +51,9 @@ def test_config_copy_mode(tmp_configuration_copy, subprocess_call, dag_maker, im
 
     tmp_configuration_copy.assert_called_with(chmod=0o600, include_env=includes, include_cmds=includes)
 
-    if impersonation:
+    if impersonation is None or impersonation == "airflow":
+        subprocess_call.not_assert_called()
+    else:
         subprocess_call.assert_called_with(
             ["sudo", "chown", impersonation, "/tmp/some-string"], close_fds=True
         )
-    else:
-        subprocess_call.not_assert_called()