Skip to content

Commit

Permalink
Cherry-pick #7594: Catch AccessDenied exception
Browse files Browse the repository at this point in the history
(cherry picked from commit 6e7654e)
  • Loading branch information
drew2a authored and kozlovsky committed Mar 8, 2024
1 parent ea9e280 commit d3bcfaf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
26 changes: 16 additions & 10 deletions src/tribler/core/check_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,28 @@ def set_process_priority(pid=None, priority_order=1):
"""
if priority_order < 0 or priority_order > 5:
return

process = psutil.Process(pid if pid else os.getpid())
if sys.platform not in {'win32', 'darwin', 'linux'}:
return

if sys.platform == 'win32':
priority_classes = [psutil.IDLE_PRIORITY_CLASS,
psutil.BELOW_NORMAL_PRIORITY_CLASS,
psutil.NORMAL_PRIORITY_CLASS,
psutil.ABOVE_NORMAL_PRIORITY_CLASS,
psutil.HIGH_PRIORITY_CLASS,
psutil.REALTIME_PRIORITY_CLASS]
process.nice(priority_classes[priority_order])
elif sys.platform == 'darwin' or sys.platform == 'linux2':
priority_classes = [
psutil.IDLE_PRIORITY_CLASS,
psutil.BELOW_NORMAL_PRIORITY_CLASS,
psutil.NORMAL_PRIORITY_CLASS,
psutil.ABOVE_NORMAL_PRIORITY_CLASS,
psutil.HIGH_PRIORITY_CLASS,
psutil.REALTIME_PRIORITY_CLASS
]
else:
# On Unix, priority can be -20 to 20, but usually not allowed to set below 0, we set our classes somewhat in
# that range.
priority_classes = [5, 4, 3, 2, 1, 0]

try:
process = psutil.Process(pid if pid else os.getpid())
process.nice(priority_classes[priority_order])
except psutil.Error as e:
logger.exception(e)


def enable_fault_handler(log_dir):
Expand Down
36 changes: 34 additions & 2 deletions src/tribler/core/tests/test_check_os.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from logging import Logger
from unittest.mock import MagicMock, patch
from unittest.mock import MagicMock, Mock, patch

from tribler.core.check_os import enable_fault_handler, error_and_exit
import psutil
import pytest

from tribler.core.check_os import enable_fault_handler, error_and_exit, set_process_priority
from tribler.core.utilities.patch_import import patch_import


Expand Down Expand Up @@ -40,3 +43,32 @@ def test_enable_fault_handler_log_dir_not_exists():

enable_fault_handler(log_dir=log_dir)
log_dir.mkdir.assert_called_once()


@patch.object(psutil.Process, 'nice')
def test_set_process_priority_supported_platform(mocked_nice: Mock):
""" Test that the process priority is set on supported platforms."""
set_process_priority()
assert mocked_nice.called


@patch('sys.platform', 'freebsd7')
@patch.object(psutil.Process, 'nice')
def test_set_process_priority_unsupported_platform(mocked_nice: Mock):
""" Test that the process priority is not set on unsupported platforms."""
set_process_priority()
assert not mocked_nice.called


def test_set_process_exception():
""" Test that the set_process_priority does not re-raise an exception derived from `psutil.Error`
but re-raise all other exceptions"""

# psutil.Error
with patch.object(psutil.Process, 'nice', new=Mock(side_effect=psutil.AccessDenied)):
set_process_priority()

# any other error
with patch.object(psutil.Process, 'nice', new=Mock(side_effect=FileNotFoundError)):
with pytest.raises(FileNotFoundError):
set_process_priority()

0 comments on commit d3bcfaf

Please sign in to comment.