From 02aecad52b92be085f5d67a2b2a89078ea2b20ce Mon Sep 17 00:00:00 2001 From: Jonathan Moody <103143855+moodyjon@users.noreply.github.com> Date: Thu, 16 Jun 2022 13:32:18 -0400 Subject: [PATCH] CancelledError derives from BaseException in Python >= 3.8. The significant functional change here is in upload_to_reflector(). Unit tests in TestReflector were failing. Deal with lint related to CancelledError cleanup. --- lbry/dht/blob_announcer.py | 2 -- lbry/extras/daemon/components.py | 2 -- lbry/file/file_manager.py | 4 +--- lbry/stream/managed_stream.py | 2 +- lbry/wallet/ledger.py | 12 +++--------- lbry/wallet/network.py | 2 +- lbry/wallet/orchstr8/node.py | 3 +-- 7 files changed, 7 insertions(+), 20 deletions(-) diff --git a/lbry/dht/blob_announcer.py b/lbry/dht/blob_announcer.py index f1194e8c5..46c74de4c 100644 --- a/lbry/dht/blob_announcer.py +++ b/lbry/dht/blob_announcer.py @@ -42,8 +42,6 @@ class BlobAnnouncer: log.debug("failed to announce %s, could only find %d peers, retrying soon.", blob_hash[:8], peers) except Exception as err: self.announcements_sent_metric.labels(peers=0, error=True).inc() - if isinstance(err, asyncio.CancelledError): # TODO: remove when updated to 3.8 - raise err log.warning("error announcing %s: %s", blob_hash[:8], str(err)) async def _announce(self, batch_size: typing.Optional[int] = 10): diff --git a/lbry/extras/daemon/components.py b/lbry/extras/daemon/components.py index 1e5e6a445..5cce7d42c 100644 --- a/lbry/extras/daemon/components.py +++ b/lbry/extras/daemon/components.py @@ -560,8 +560,6 @@ class UPnPComponent(Component): self.upnp = await UPnP.discover(loop=self.component_manager.loop) log.info("found upnp gateway: %s", self.upnp.gateway.manufacturer_string) except Exception as err: - if isinstance(err, asyncio.CancelledError): # TODO: remove when updated to 3.8 - raise log.warning("upnp discovery failed: %s", err) self.upnp = None diff --git a/lbry/file/file_manager.py b/lbry/file/file_manager.py index 6ab943907..c3ef9106c 100644 --- a/lbry/file/file_manager.py +++ b/lbry/file/file_manager.py @@ -99,8 +99,6 @@ class FileManager: except asyncio.TimeoutError: raise ResolveTimeoutError(uri) except Exception as err: - if isinstance(err, asyncio.CancelledError): - raise log.exception("Unexpected error resolving stream:") raise ResolveError(f"Unexpected error resolving stream: {str(err)}") if 'error' in resolved_result: @@ -249,7 +247,7 @@ class FileManager: except asyncio.TimeoutError: error = DownloadDataTimeoutError(stream.sd_hash) raise error - except Exception as err: # forgive data timeout, don't delete stream + except (Exception, asyncio.CancelledError) as err: # forgive data timeout, don't delete stream expected = (DownloadSDTimeoutError, DownloadDataTimeoutError, InsufficientFundsError, KeyFeeAboveMaxAllowedError, ResolveError, InvalidStreamURLError) if isinstance(err, expected): diff --git a/lbry/stream/managed_stream.py b/lbry/stream/managed_stream.py index 7a4b69093..38f47a3a3 100644 --- a/lbry/stream/managed_stream.py +++ b/lbry/stream/managed_stream.py @@ -367,7 +367,7 @@ class ManagedStream(ManagedDownloadSource): return sent except ConnectionError: return sent - except (OSError, Exception) as err: + except (OSError, Exception, asyncio.CancelledError) as err: if isinstance(err, asyncio.CancelledError): log.warning("stopped uploading %s#%s to reflector", self.claim_name, self.claim_id) elif isinstance(err, OSError): diff --git a/lbry/wallet/ledger.py b/lbry/wallet/ledger.py index 42499d2c6..477c19f9c 100644 --- a/lbry/wallet/ledger.py +++ b/lbry/wallet/ledger.py @@ -938,9 +938,7 @@ class Ledger(metaclass=LedgerRegistry): "%d change addresses (gap: %d), %d channels, %d certificates and %d claims. ", account.id, balance, total_receiving, account.receiving.gap, total_change, account.change.gap, channel_count, len(account.channel_keys), claim_count) - except Exception as err: - if isinstance(err, asyncio.CancelledError): # TODO: remove when updated to 3.8 - raise + except Exception: log.exception( 'Failed to display wallet state, please file issue ' 'for this bug along with the traceback you see below:') @@ -963,9 +961,7 @@ class Ledger(metaclass=LedgerRegistry): claim_ids = [p.purchased_claim_id for p in purchases] try: resolved, _, _, _ = await self.claim_search([], claim_ids=claim_ids) - except Exception as err: - if isinstance(err, asyncio.CancelledError): # TODO: remove when updated to 3.8 - raise + except Exception: log.exception("Resolve failed while looking up purchased claim ids:") resolved = [] lookup = {claim.claim_id: claim for claim in resolved} @@ -1045,9 +1041,7 @@ class Ledger(metaclass=LedgerRegistry): claim_ids = collection.claim.collection.claims.ids[offset:page_size + offset] try: resolve_results, _, _, _ = await self.claim_search([], claim_ids=claim_ids) - except Exception as err: - if isinstance(err, asyncio.CancelledError): # TODO: remove when updated to 3.8 - raise + except Exception: log.exception("Resolve failed while looking up collection claim ids:") return [] claims = [] diff --git a/lbry/wallet/network.py b/lbry/wallet/network.py index 55b8b145b..18804608f 100644 --- a/lbry/wallet/network.py +++ b/lbry/wallet/network.py @@ -117,7 +117,7 @@ class ClientSession(BaseClientSession): ) else: await asyncio.sleep(max(0, max_idle - (now - self.last_send))) - except Exception as err: + except (Exception, asyncio.CancelledError) as err: if isinstance(err, asyncio.CancelledError): log.info("closing connection to %s:%i", *self.server) else: diff --git a/lbry/wallet/orchstr8/node.py b/lbry/wallet/orchstr8/node.py index ea041802a..ade7ca610 100644 --- a/lbry/wallet/orchstr8/node.py +++ b/lbry/wallet/orchstr8/node.py @@ -264,8 +264,7 @@ class SPVNode: await self.server.start() except Exception as e: self.stopped = True - if not isinstance(e, asyncio.CancelledError): - log.exception("failed to start spv node") + log.exception("failed to start spv node") raise e async def stop(self, cleanup=True):