Skip to content

Commit

Permalink
Mark current process with matching pid to be running
Browse files Browse the repository at this point in the history
  • Loading branch information
xoriole committed Oct 10, 2023
1 parent acd46e2 commit 2516505
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/tribler/core/utilities/process_manager/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -159,6 +159,9 @@ def become_primary(self) -> bool:

def is_running(self):
"""Returns True if the object represents a running process"""
if self.is_current_process():
return True

if not psutil.pid_exists(self.pid):
return False

Expand Down
29 changes: 20 additions & 9 deletions src/tribler/core/utilities/process_manager/tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,48 @@ def current_process_fixture(process_manager):
return process_manager.current_process


@pytest.fixture(name='non_current_process')
def non_current_process_fixture(process_manager: ProcessManager):
process_manager.connection = Mock()
process_manager.current_process.is_current_process = lambda: False
return process_manager.current_process


def test_current_process_is_running(current_process):
assert current_process.is_running() is True


@patch('psutil.pid_exists')
def test_is_running_pid_does_not_exists(pid_exists: Mock, current_process):
def test_is_running_pid_does_not_exists(pid_exists: Mock, non_current_process):
pid_exists.return_value = False
# if the pid does not exist, the process is not running
assert not pid_exists.called
assert current_process.is_running() is False
assert non_current_process.is_running() is False
assert pid_exists.called


@patch('psutil.Process')
def test_is_running_process_not_running(process_class: Mock, current_process):
def test_is_running_process_not_running(process_class: Mock, non_current_process):
process_class.side_effect = psutil.Error
# if the instantiation of the Process instance lead to psutil.Error, the process is not running
assert current_process.is_running() is False
assert non_current_process.is_running() is False
assert process_class.called


@patch('psutil.Process')
def test_is_running_zombie_process(process_class: Mock, current_process):
def test_is_running_zombie_process(process_class: Mock, non_current_process):
process_class.return_value.status.return_value = psutil.STATUS_ZOMBIE
# if the process is zombie, it is not considered to be running
assert current_process.is_running() is False
assert non_current_process.is_running() is False


@patch('psutil.Process')
def test_is_running_incorrect_process_create_time(process_class: Mock, current_process):
def test_is_running_incorrect_process_create_time(process_class: Mock, non_current_process):
process = process_class.return_value
process.status.return_value = psutil.STATUS_RUNNING
process.create_time.return_value = current_process.started_at + 1
process.create_time.return_value = non_current_process.started_at + 1
# if the process with the specified pid was created after the specified time, it is a different process
assert current_process.is_running() is False
assert non_current_process.is_running() is False


@patch('psutil.Process')
Expand Down

0 comments on commit 2516505

Please sign in to comment.