Skip to content

Commit

Permalink
Handle the possible RuntimeException when handle.file_progress() is c…
Browse files Browse the repository at this point in the history
…alled (#7678)
  • Loading branch information
qstokkink committed Nov 8, 2023
1 parent 9c950e8 commit 4fd1c8a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ def get_files_completion(self):

if self.lt_status and self.download.handle and self.download.handle.is_valid():
files = self.download.get_def().get_files_with_length()
progress = self.download.handle.file_progress(flags=1)
try:
progress = self.download.handle.file_progress(flags=1)
except RuntimeError:
# For large torrents, the handle can be invalid at this point.
# See https://github.com/Tribler/tribler/issues/6454
progress = None
if progress and len(progress) == len(files):
for index, (path, size) in enumerate(files):
completion_frac = (float(progress[index]) / size) if size > 0 else 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
from tribler.core.utilities.simpledefs import DOWNLOAD, DownloadStatus, UPLOAD


@pytest.fixture
def mock_tdef():
@pytest.fixture(name="mock_tdef")
def fixture_mock_tdef():
mocked_tdef = Mock()
mocked_tdef.get_name = lambda: "test"
mocked_tdef.get_length = lambda: 43
return mocked_tdef


@pytest.fixture
def mock_download(mock_tdef):
@pytest.fixture(name="mock_download")
def fixture_mock_download(mock_tdef):
mock_download = Mock()
mock_download.get_def = lambda: mock_tdef
return mock_download
Expand Down Expand Up @@ -121,3 +121,23 @@ def test_get_availability(mock_download):
download_state.get_peerlist = lambda: [{'completed': 0.5, 'have': [1, 0]},
{'completed': 0.9, 'have': [1, 0, 1]}]
assert download_state.get_availability() == 0.0


def test_get_files_completion_semivalid_handle(mock_download, mock_tdef):
"""
Testing whether no file completion is returned for valid handles that have invalid file_progress.
This case mirrors https://github.com/Tribler/tribler/issues/6454
"""
mock_tdef.get_files_with_length = lambda: [("test.txt", 100)]

def file_progress(flags: int):
raise RuntimeError("invalid torrent handle used")

handle = Mock()
handle.file_progress = file_progress
handle.is_valid = lambda: True
mock_download.handle = handle

download_state = DownloadState(mock_download, Mock(), None)
assert download_state.get_files_completion() == []

0 comments on commit 4fd1c8a

Please sign in to comment.