Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pid check #24636

Merged
merged 6 commits into from
Jul 11, 2022
Merged

Fix pid check #24636

merged 6 commits into from
Jul 11, 2022

Conversation

krcrouse
Copy link
Contributor

@krcrouse krcrouse commented Jun 24, 2022

related: #17507
related: #20992

This fixes issues in which running a task with impersonation leads to the error Recorded pid {PID1} does not match the current pid {PID2}, which then triggers a SIGTERM call to all processes in the group. The issue appears to be that in some cases the recorded pid (which is also the taskinstance pid) is None, which leads the ensuing psutils.Process(ti.pid).ppid() call to return the parent of the current running process instead of the parent of the taskinstance - and the parent is the long running Worker, which is not the desired process to identify.

I was able to reproduce this error regularly and separately traced all processes to try to identify what they were. The "current pid" is the task runner, and in most cases the relevant call was so short lived I couldn't even get it in my tracing - which would explain why it was no longer registered to the task instance.

@boring-cyborg boring-cyborg bot added the area:Scheduler including HA (high availability) scheduler label Jun 24, 2022
@boring-cyborg
Copy link

boring-cyborg bot commented Jun 24, 2022

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contribution Guide (https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst)
Here are some useful points:

  • Pay attention to the quality of your code (flake8, mypy and type annotations). Our pre-commits will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example DAG that shows how users should use it.
  • Consider using Breeze environment for testing locally, it’s a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: dev@airflow.apache.org
    Slack: https://s.apache.org/airflow-slack

@uranusjr
Copy link
Member

Is it possible to have a test for this?

@kagesenshi
Copy link

kagesenshi commented Jun 29, 2022

I tried applying this on my 2.2.5 install,

no improvement on my side. Adding a little print logging of ti.pid, current_pid and psutil.Process(ti.pid).ppid() shows that t.pid is same as current pid, but not the same as parent pid (eg: t.pid is 39, current_pid is 39 and ppid() is 35).

I noticed that when dag code does not have run_as_user, but airflow.cfg have default_impersonation set, ti.run_as_user or self.task_runner.run_as_user seems to evaluates to True, which caused airflow to compare current_pid with ppid() instead of with t.pid

@krcrouse
Copy link
Contributor Author

krcrouse commented Jul 7, 2022

Is it possible to have a test for this?

I just added a set of tests for this to look at when the ti.pid is None based on the different contexts of when run_as_user is not set, and in the 2 cases in which it is set. Note that only the first test currently passes on main, showing the need for this PR.

@krcrouse
Copy link
Contributor Author

krcrouse commented Jul 7, 2022

@kagesenshi, that sounds like a different issue and I haven't seen that one. This specifically addresses when the Task Instance PID is unset.

Copy link
Member

@uranusjr uranusjr left a comment

Choose a reason for hiding this comment

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

Makes sense to me

Copy link
Member

@potiuk potiuk left a comment

Choose a reason for hiding this comment

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

Seems plausible and very interesting race condition.

@ashb WDYT?

@potiuk potiuk merged commit 26c9768 into apache:main Jul 11, 2022
@boring-cyborg
Copy link

boring-cyborg bot commented Jul 11, 2022

Awesome work, congrats on your first merged pull request!

@ephraimbuddy ephraimbuddy added the type:bug-fix Changelog: Bug Fixes label Aug 14, 2022
@ephraimbuddy ephraimbuddy added this to the Airflow 2.3.4 milestone Aug 19, 2022
ephraimbuddy pushed a commit that referenced this pull request Aug 19, 2022
(cherry picked from commit 26c9768)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:Scheduler including HA (high availability) scheduler type:bug-fix Changelog: Bug Fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants