Skip to content

Commit

Permalink
Merge pull request #7415 from xoriole/fix/invalid-hadle
Browse files Browse the repository at this point in the history
Fix invalid handle when there is no torrent metadata
  • Loading branch information
xoriole authored May 11, 2023
2 parents 1434827 + ac923ca commit 339d27a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/tribler/core/components/libtorrent/tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from tribler.core.components.libtorrent.download_manager.download import Download
from tribler.core.components.libtorrent.download_manager.download_config import DownloadConfig
from tribler.core.components.libtorrent.torrentdef import TorrentDefNoMetainfo
from tribler.core.components.libtorrent.utils.torrent_utils import get_info_from_handle
from tribler.core.components.reporter.exception_handler import NoCrashException
from tribler.core.exceptions import SaveResumeDataError
Expand Down Expand Up @@ -409,6 +410,21 @@ def test_on_state_changed(mock_handle, test_download):
test_download.apply_ip_filter.assert_called_with(False)


async def test_apply_ip_filter(test_download, mock_handle): # pylint: disable=unused-argument
test_download.handle.status = lambda: Mock(error=None)
test_download.tdef.get_infohash = lambda: b'a' * 20
test_download.config.set_hops(1)

assert not isinstance(test_download.tdef, TorrentDefNoMetainfo)
await test_download.apply_ip_filter(True)
test_download.handle.apply_ip_filter.assert_called_with(True)

test_download.tdef = TorrentDefNoMetainfo(b'a' * 20, 'metainfo request')
test_download.handle.reset_mock()
test_download.apply_ip_filter(False)
test_download.handle.apply_ip_filter.assert_not_called()


async def test_checkpoint_timeout(test_download):
"""
Testing whether making a checkpoint times out when we receive no alert from libtorrent
Expand Down
5 changes: 3 additions & 2 deletions src/tribler/core/components/libtorrent/torrentdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import aiohttp

from tribler.core.components.libtorrent.utils.libtorrent_helper import libtorrent as lt
from tribler.core.components.libtorrent.utils.torrent_utils import create_torrent_file
from tribler.core.components.libtorrent.utils import torrent_utils
from tribler.core.utilities import maketorrent, path_util
from tribler.core.utilities.path_util import Path
from tribler.core.utilities.simpledefs import INFOHASH_LENGTH
Expand Down Expand Up @@ -317,7 +317,8 @@ def save(self, torrent_filepath=None):
Generate the metainfo and save the torrent file.
:param torrent_filepath: An optional absolute path to where to save the generated .torrent file.
"""
torrent_dict = create_torrent_file(self.files_list, self.torrent_parameters, torrent_filepath=torrent_filepath)
torrent_dict = torrent_utils.create_torrent_file(self.files_list, self.torrent_parameters,
torrent_filepath=torrent_filepath)
self.metainfo = bdecode_compat(torrent_dict['metainfo'])
self.copy_metainfo_to_torrent_parameters()
self.infohash = torrent_dict['infohash']
Expand Down
9 changes: 7 additions & 2 deletions src/tribler/core/components/libtorrent/utils/torrent_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from hashlib import sha1
from typing import Any, Dict, Iterable, List, Optional

from tribler.core.components.libtorrent import torrentdef
from tribler.core.components.libtorrent.utils.libtorrent_helper import libtorrent as lt
from tribler.core.utilities.path_util import Path

Expand Down Expand Up @@ -33,14 +34,18 @@ def require_handle(func):
Invoke the function once the handle is available. Returns a future that will fire once the function has completed.
Author(s): Egbert Bouman
"""

def invoke_func(*args, **kwargs):
result_future = Future()

def done_cb(fut):
with suppress(CancelledError):
handle = fut.result()
if not fut.cancelled() and not result_future.done() and handle == download.handle and handle.is_valid():

if not fut.cancelled() \
and not result_future.done() \
and handle == download.handle \
and handle.is_valid() \
and not isinstance(download.tdef, torrentdef.TorrentDefNoMetainfo):
result_future.set_result(func(*args, **kwargs))

download = args[0]
Expand Down

0 comments on commit 339d27a

Please sign in to comment.