From d7abc40bb55d439a2cf15e149b176c0449dc510d Mon Sep 17 00:00:00 2001 From: Sandip Pandey Date: Thu, 12 Oct 2023 15:17:40 +0200 Subject: [PATCH 1/2] Check for current process when checking primary process --- src/tribler/core/utilities/process_manager/manager.py | 2 +- src/tribler/core/utilities/process_manager/process.py | 2 +- .../core/utilities/process_manager/tests/test_manager.py | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/tribler/core/utilities/process_manager/manager.py b/src/tribler/core/utilities/process_manager/manager.py index 37f18ccd06c..f583091170f 100644 --- a/src/tribler/core/utilities/process_manager/manager.py +++ b/src/tribler/core/utilities/process_manager/manager.py @@ -119,7 +119,7 @@ def primary_process_rowid(self, kind: ProcessKind) -> Optional[int]: row = cursor.fetchone() if row is not None: process = TriblerProcess.from_row(self, row) - if process.is_running(): + if process.is_current_process() or process.is_running(): return process.rowid # Process is not running anymore; mark it as not primary diff --git a/src/tribler/core/utilities/process_manager/process.py b/src/tribler/core/utilities/process_manager/process.py index 875d5c43754..1e074c7c7d6 100644 --- a/src/tribler/core/utilities/process_manager/process.py +++ b/src/tribler/core/utilities/process_manager/process.py @@ -140,7 +140,7 @@ def current_process(cls, kind: ProcessKind, def is_current_process(self) -> bool: """Returns True if the object represents the current process""" - return self.pid == os.getpid() and self.is_running() + return self.pid == os.getpid() @with_retry def become_primary(self) -> bool: diff --git a/src/tribler/core/utilities/process_manager/tests/test_manager.py b/src/tribler/core/utilities/process_manager/tests/test_manager.py index 70002390280..d09875cb8ea 100644 --- a/src/tribler/core/utilities/process_manager/tests/test_manager.py +++ b/src/tribler/core/utilities/process_manager/tests/test_manager.py @@ -11,7 +11,8 @@ # pylint: disable=protected-access -def test_become_primary(process_manager: ProcessManager): +@patch('os.getpid', return_value=1) +def test_become_primary(os_getpid: Mock, process_manager: ProcessManager): # Initially process manager fixture creates a primary current process that is a single process in DB p1 = process_manager.current_process assert p1.primary From 9e6ed3d1ecd43333898f8f83ef115f303a98c303 Mon Sep 17 00:00:00 2001 From: Sandip Pandey Date: Thu, 12 Oct 2023 16:09:19 +0200 Subject: [PATCH 2/2] Update os.getpid patch with new arg --- .../core/utilities/process_manager/tests/test_manager.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tribler/core/utilities/process_manager/tests/test_manager.py b/src/tribler/core/utilities/process_manager/tests/test_manager.py index d09875cb8ea..5ec9ef77c3b 100644 --- a/src/tribler/core/utilities/process_manager/tests/test_manager.py +++ b/src/tribler/core/utilities/process_manager/tests/test_manager.py @@ -11,8 +11,8 @@ # pylint: disable=protected-access -@patch('os.getpid', return_value=1) -def test_become_primary(os_getpid: Mock, process_manager: ProcessManager): +@patch('os.getpid', new=lambda: 1) +def test_become_primary(process_manager: ProcessManager): # Initially process manager fixture creates a primary current process that is a single process in DB p1 = process_manager.current_process assert p1.primary