From 77dbc05e5928d207c826afd09430b6172c34591d Mon Sep 17 00:00:00 2001 From: John Lewis <9gj2mk85rq@snkmail.com> Date: Sun, 5 May 2024 06:58:55 -0400 Subject: [PATCH] don't kill if pid same as file (#8997) (#8998) * don't kill if pid same as file (#8997) * test for don't kill if pid same as file (#8997) * restore file permission --------- Co-authored-by: Asif Saif Uddin --- celery/platforms.py | 3 +++ t/unit/utils/test_platforms.py | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/celery/platforms.py b/celery/platforms.py index 6203f2c29b5..1375fd82c0b 100644 --- a/celery/platforms.py +++ b/celery/platforms.py @@ -186,6 +186,9 @@ def remove_if_stale(self): if not pid: self.remove() return True + if pid == os.getpid(): + # this can be common in k8s pod with PID of 1 - don't kill + return True try: os.kill(pid, 0) diff --git a/t/unit/utils/test_platforms.py b/t/unit/utils/test_platforms.py index ab1a9436543..3f4e47ae339 100644 --- a/t/unit/utils/test_platforms.py +++ b/t/unit/utils/test_platforms.py @@ -689,6 +689,15 @@ def test_remove_if_stale_no_pidfile(self): assert p.remove_if_stale() p.remove.assert_called_with() + def test_remove_if_stale_same_pid(self): + p = Pidfile('/var/pid') + p.read_pid = Mock() + p.read_pid.return_value = os.getpid() + p.remove = Mock() + + assert p.remove_if_stale() + p.remove.assert_not_called() + @patch('os.fsync') @patch('os.getpid') @patch('os.open')