Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(...)`.

## 2026-04-03 - Optimizing the first matching concurrent tasks
**Learning:** `streamrip/client/qobuz.py` checks multiple potential app secrets to find one that works. Previously it used `asyncio.gather`, which waited for *all* requests to complete (even the slow timeouts or failing ones) before checking the results and taking the first valid one. Since we only need the *first* successful response, `asyncio.as_completed` allows us to short-circuit the execution immediately when the first valid secret is found, drastically reducing latency by not waiting for the slowest request.
**Action:** Use `asyncio.as_completed` with task cancellation (`try...finally` block cancelling all unfinished tasks) when concurrently fetching data where only the first valid response is needed.
20 changes: 12 additions & 8 deletions streamrip/client/qobuz.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,14 +409,18 @@ 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:
raise InvalidAppSecretError(secrets)

return working_secrets[0]
tasks = [asyncio.create_task(self._test_secret(secret)) for secret in secrets]
try:
for future in asyncio.as_completed(tasks):
result = await future
if result is not None:
return result
finally:
for task in tasks:
if not task.done():
task.cancel()
Comment on lines +419 to +421
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot Apr 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Cancelled tasks are not awaited, which can leak HTTP connections and produce runtime warnings. After cancelling, await the tasks to ensure their coroutines run cleanup code (e.g., closing aiohttp response context managers).

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At streamrip/client/qobuz.py, line 419:

<comment>Cancelled tasks are not awaited, which can leak HTTP connections and produce runtime warnings. After cancelling, await the tasks to ensure their coroutines run cleanup code (e.g., closing `aiohttp` response context managers).</comment>

<file context>
@@ -409,14 +409,18 @@ async def _test_secret(self, secret: str) -> Optional[str]:
+                if result is not None:
+                    return result
+        finally:
+            for task in tasks:
+                if not task.done():
+                    task.cancel()
</file context>
Suggested change
for task in tasks:
if not task.done():
task.cancel()
for task in tasks:
if not task.done():
task.cancel()
await asyncio.gather(*tasks, return_exceptions=True)
Fix with Cubic


raise InvalidAppSecretError(secrets)

async def _request_file_url(
self,
Expand Down