Skip to content

Commit 7d01f16

Browse files
committed
generalize DownloadSDTimeout to DownloadMetadata timeout + fix usages
1 parent 952e05e commit 7d01f16

File tree

8 files changed

+26
-20
lines changed

8 files changed

+26
-20
lines changed

lbry/error/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ Code | Name | Message
8181
511 | CorruptBlob | Blobs is corrupted.
8282
520 | BlobFailedEncryption | Failed to encrypt blob.
8383
531 | DownloadCancelled | Download was canceled.
84-
532 | DownloadSDTimeout | Failed to download sd blob {download} within timeout.
85-
533 | DownloadDataTimeout | Failed to download data blobs for sd hash {download} within timeout.
84+
532 | DownloadMetadataTimeout | Failed to download metadata for {download} within timeout.
85+
533 | DownloadDataTimeout | Failed to download data blobs for {download} within timeout.
8686
534 | InvalidStreamDescriptor | {message}
8787
535 | InvalidData | {message}
8888
536 | InvalidBlobHash | {message}

lbry/error/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,18 +411,18 @@ def __init__(self):
411411
super().__init__("Download was canceled.")
412412

413413

414-
class DownloadSDTimeoutError(BlobError):
414+
class DownloadMetadataTimeoutError(BlobError):
415415

416416
def __init__(self, download):
417417
self.download = download
418-
super().__init__(f"Failed to download sd blob {download} within timeout.")
418+
super().__init__(f"Failed to download metadata for {download} within timeout.")
419419

420420

421421
class DownloadDataTimeoutError(BlobError):
422422

423423
def __init__(self, download):
424424
self.download = download
425-
super().__init__(f"Failed to download data blobs for sd hash {download} within timeout.")
425+
super().__init__(f"Failed to download data blobs for {download} within timeout.")
426426

427427

428428
class InvalidStreamDescriptorError(BlobError):

lbry/extras/daemon/daemon.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from lbry.blob_exchange.downloader import download_blob
3737
from lbry.dht.peer import make_kademlia_peer
3838
from lbry.error import (
39-
DownloadSDTimeoutError, ComponentsNotStartedError, ComponentStartConditionNotMetError,
39+
DownloadMetadataTimeoutError, ComponentsNotStartedError, ComponentStartConditionNotMetError,
4040
CommandDoesNotExistError, BaseError, WalletNotFoundError, WalletAlreadyLoadedError, WalletAlreadyExistsError,
4141
ConflictingInputValueError, AlreadyPurchasedError, PrivateKeyNotFoundError, InputStringIsBlankError,
4242
InputValueError
@@ -1140,7 +1140,7 @@ async def jsonrpc_get(
11401140
save_file=save_file, wallet=wallet
11411141
)
11421142
if not stream:
1143-
raise DownloadSDTimeoutError(uri)
1143+
raise DownloadMetadataTimeoutError(uri)
11441144
except Exception as e:
11451145
# TODO: use error from lbry.error
11461146
log.warning("Error downloading %s: %s", uri, str(e))

lbry/file/file_manager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import typing
44
from typing import Optional
55
from aiohttp.web import Request
6-
from lbry.error import ResolveError, DownloadSDTimeoutError, InsufficientFundsError
6+
from lbry.error import ResolveError, DownloadMetadataTimeoutError, InsufficientFundsError
77
from lbry.error import ResolveTimeoutError, DownloadDataTimeoutError, KeyFeeAboveMaxAllowedError
88
from lbry.error import InvalidStreamURLError
99
from lbry.stream.managed_stream import ManagedStream
@@ -244,10 +244,10 @@ async def download_from_uri(self, uri, exchange_rate_manager: 'ExchangeRateManag
244244
loop=self.loop)
245245
return stream
246246
except asyncio.TimeoutError:
247-
error = DownloadDataTimeoutError(stream.sd_hash)
247+
error = DownloadDataTimeoutError(stream.identifier)
248248
raise error
249249
except Exception as err: # forgive data timeout, don't delete stream
250-
expected = (DownloadSDTimeoutError, DownloadDataTimeoutError, InsufficientFundsError,
250+
expected = (DownloadMetadataTimeoutError, DownloadDataTimeoutError, InsufficientFundsError,
251251
KeyFeeAboveMaxAllowedError, ResolveError, InvalidStreamURLError)
252252
if isinstance(err, expected):
253253
log.warning("Failed to download %s: %s", uri, str(err))

lbry/stream/downloader.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import binascii
55

66
from lbry.dht.node import get_kademlia_peers_from_hosts
7-
from lbry.error import DownloadSDTimeoutError
7+
from lbry.error import DownloadMetadataTimeoutError
88
from lbry.utils import lru_cache_concurrent
99
from lbry.stream.descriptor import StreamDescriptor
1010
from lbry.blob_exchange.downloader import BlobDownloader
@@ -77,7 +77,7 @@ async def load_descriptor(self, connection_id: int = 0):
7777
log.info("downloaded sd blob %s", self.sd_hash)
7878
self.time_to_descriptor = self.loop.time() - now
7979
except asyncio.TimeoutError:
80-
raise DownloadSDTimeoutError(self.sd_hash)
80+
raise DownloadMetadataTimeoutError(self.sd_hash)
8181

8282
# parse the descriptor
8383
self.descriptor = await StreamDescriptor.from_stream_descriptor_blob(

lbry/stream/managed_stream.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import logging
66
from typing import Optional
77
from aiohttp.web import Request, StreamResponse, HTTPRequestRangeNotSatisfiable
8-
from lbry.error import DownloadSDTimeoutError
8+
from lbry.error import DownloadMetadataTimeoutError
99
from lbry.schema.mime_types import guess_media_type
1010
from lbry.stream.downloader import StreamDownloader
1111
from lbry.stream.descriptor import StreamDescriptor, sanitize_file_name
@@ -148,7 +148,7 @@ async def start(self, timeout: Optional[float] = None,
148148
await asyncio.wait_for(self.downloader.start(), timeout, loop=self.loop)
149149
except asyncio.TimeoutError:
150150
self._running.clear()
151-
raise DownloadSDTimeoutError(self.sd_hash)
151+
raise DownloadMetadataTimeoutError(self.identifier)
152152

153153
if self.delayed_stop_task and not self.delayed_stop_task.done():
154154
self.delayed_stop_task.cancel()

lbry/torrent/torrent_manager.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing import Optional
88
from aiohttp.web import Request, StreamResponse, HTTPRequestRangeNotSatisfiable
99

10+
from lbry.error import DownloadMetadataTimeoutError
1011
from lbry.file.source_manager import SourceManager
1112
from lbry.file.source import ManagedDownloadSource
1213
from lbry.schema.mime_types import guess_media_type
@@ -57,7 +58,12 @@ def mime_type(self) -> Optional[str]:
5758
return guess_media_type(os.path.basename(self.full_path))[0]
5859

5960
async def start(self, timeout: Optional[float] = None, save_now: Optional[bool] = False):
60-
await self.torrent_session.add_torrent(self.identifier, self.download_directory)
61+
try:
62+
metadata_download = self.torrent_session.add_torrent(self.identifier, self.download_directory)
63+
await asyncio.wait_for(metadata_download, timeout, loop=self.loop)
64+
except asyncio.TimeoutError:
65+
self.torrent_session.remove_torrent(btih=self.identifier)
66+
raise DownloadMetadataTimeoutError(self.identifier)
6167
self.download_directory = self.torrent_session.save_path(self.identifier)
6268
self._file_name = Path(self.torrent_session.full_path(self.identifier)).name
6369
await self.storage.add_torrent(self.identifier, self.torrent_length, self.torrent_name)

tests/unit/stream/test_stream_manager.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from lbry.testcase import get_fake_exchange_rate_manager
1212
from lbry.utils import generate_id
1313
from lbry.error import InsufficientFundsError
14-
from lbry.error import KeyFeeAboveMaxAllowedError, ResolveError, DownloadSDTimeoutError, DownloadDataTimeoutError
14+
from lbry.error import KeyFeeAboveMaxAllowedError, ResolveError, DownloadMetadataTimeoutError, DownloadDataTimeoutError
1515
from lbry.wallet import WalletManager, Wallet, Ledger, Transaction, Input, Output, Database
1616
from lbry.wallet.constants import CENT, NULL_HASH32
1717
from lbry.wallet.network import ClientSession
@@ -232,7 +232,7 @@ def check_post(event):
232232
event['properties']['error_message'], f'Failed to download sd blob {self.sd_hash} within timeout.'
233233
)
234234

235-
await self._test_time_to_first_bytes(check_post, DownloadSDTimeoutError, after_setup=after_setup)
235+
await self._test_time_to_first_bytes(check_post, DownloadMetadataTimeoutError, after_setup=after_setup)
236236

237237
async def test_override_fixed_peer_delay_dht_disabled(self):
238238
self.client_config.fixed_peers = [(self.server_from_client.address, self.server_from_client.tcp_port)]
@@ -266,7 +266,7 @@ async def test_no_peers_timeout(self):
266266

267267
def check_post(event):
268268
self.assertEqual(event['event'], 'Time To First Bytes')
269-
self.assertEqual(event['properties']['error'], 'DownloadSDTimeoutError')
269+
self.assertEqual(event['properties']['error'], 'DownloadMetadataTimeoutError')
270270
self.assertEqual(event['properties']['tried_peers_count'], 0)
271271
self.assertEqual(event['properties']['active_peer_count'], 0)
272272
self.assertFalse(event['properties']['use_fixed_peers'])
@@ -277,7 +277,7 @@ def check_post(event):
277277
)
278278

279279
start = self.loop.time()
280-
await self._test_time_to_first_bytes(check_post, DownloadSDTimeoutError)
280+
await self._test_time_to_first_bytes(check_post, DownloadMetadataTimeoutError)
281281
duration = self.loop.time() - start
282282
self.assertLessEqual(duration, 5)
283283
self.assertGreaterEqual(duration, 3.0)
@@ -387,7 +387,7 @@ async def test_download_sd_timeout(self):
387387
self.server.stop_server()
388388
await self.setup_stream_manager()
389389
await self._test_download_error_analytics_on_start(
390-
DownloadSDTimeoutError, f'Failed to download sd blob {self.sd_hash} within timeout.', timeout=1
390+
DownloadMetadataTimeoutError, f'Failed to download sd blob {self.sd_hash} within timeout.', timeout=1
391391
)
392392

393393
async def test_download_data_timeout(self):

0 commit comments

Comments
 (0)