From df04e0390c88d92c4521afa67cffc37ef579f95d Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 31 Mar 2026 22:13:59 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Use=20as=5Fcompleted=20for?= =?UTF-8?q?=20early=20exit=20in=20Qobuz=20app=20secret=20check?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces `asyncio.gather` with `asyncio.as_completed` in `_get_valid_secret` to short-circuit as soon as a valid secret is found. Pending tasks are cancelled in a `finally` block to prevent background task leakage and unnecessary network requests. Co-authored-by: davidjuarezdev <230496599+davidjuarezdev@users.noreply.github.com> --- .jules/bolt.md | 4 ++++ streamrip/client/qobuz.py | 19 ++++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index babd2024..3703d2bf 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -5,3 +5,7 @@ ## 2026-03-20 - Non-blocking I/O in Deezer client **Learning:** `streamrip/client/deezer.py` uses the synchronous `deezer-python` library. Direct calls like `client.gw.get_track()` and `client.get_track_url()` block the entire `asyncio` event loop. While the metadata fetching methods (`get_track`, `get_album`, etc.) correctly wrapped these calls in `await asyncio.to_thread(...)`, `get_downloadable` missed this, causing heavy blocking during concurrent downloads. **Action:** Ensure all synchronous third-party API calls in async methods are wrapped with `await asyncio.to_thread(...)`. + +## 2024-03-31 - Network Requests early exit via as_completed +**Learning:** Using `asyncio.gather` for checking API secrets sequentially blocked execution until the slowest request finished (or failed), even if a valid secret was found instantly. Task cancellation combined with `asyncio.as_completed` prevents slow-tail latency and leakage of background tasks. +**Action:** When validating a list of items where only the first success matters, always use `asyncio.as_completed` and cancel remaining tasks within a `finally` block instead of waiting for all with `asyncio.gather`. diff --git a/streamrip/client/qobuz.py b/streamrip/client/qobuz.py index 734e2b82..0ebf32ab 100644 --- a/streamrip/client/qobuz.py +++ b/streamrip/client/qobuz.py @@ -409,14 +409,19 @@ async def _test_secret(self, secret: str) -> Optional[str]: return None async def _get_valid_secret(self, secrets: list[str]) -> str: - results = await asyncio.gather( - *[self._test_secret(secret) for secret in secrets], - ) - working_secrets = [r for r in results if r is not None] - if len(working_secrets) == 0: + # ⚡ Bolt: Use as_completed to early exit as soon as a working secret is found + tasks = [asyncio.create_task(self._test_secret(secret)) for secret in secrets] + try: + for coro in asyncio.as_completed(tasks): + result = await coro + if result is not None: + return result raise InvalidAppSecretError(secrets) - - return working_secrets[0] + finally: + # Cancel any remaining tasks to save network requests and prevent slow-tail latency + for task in tasks: + if not task.done(): + task.cancel() async def _request_file_url( self,