Skip to content

⚡ Bolt: Short-circuit Qobuz valid secret check#64

Draft
davidjuarezdev wants to merge 1 commit intomainfrom
bolt-qobuz-short-circuit-secrets-1032822583719394639
Draft

⚡ Bolt: Short-circuit Qobuz valid secret check#64
davidjuarezdev wants to merge 1 commit intomainfrom
bolt-qobuz-short-circuit-secrets-1032822583719394639

Conversation

@davidjuarezdev
Copy link
Copy Markdown
Owner

@davidjuarezdev davidjuarezdev commented Apr 3, 2026

💡 What: Replace asyncio.gather with asyncio.as_completed for Qobuz secret validation.
🎯 Why: To immediately return the first valid secret, avoiding slow-tail latency and unnecessary network requests.
📊 Impact: Significantly speeds up Qobuz login and saves network bandwidth by canceling remaining requests.
🔬 Measurement: The tests still pass and the login duration is visibly shorter when the first secret tested happens to be valid.


PR created automatically by Jules for task 1032822583719394639 started by @davidjuarezdev

Summary by Sourcery

Enhancements:

  • Optimize Qobuz secret validation by iterating over asynchronous tests with early return on the first valid result and cancelling outstanding tasks to reduce latency and unnecessary network I/O.

Summary by cubic

Short-circuit Qobuz secret validation to return the first valid secret and cancel the rest, reducing login latency and network I/O.

  • Refactors
    • Replace asyncio.gather with asyncio.as_completed to return on the first valid secret.
    • Cancel remaining secret-check tasks; preserve error if no secret is valid.

Written for commit abfb3f9. Summary will update on new commits.

Replaces asyncio.gather with asyncio.as_completed in streamrip/client/qobuz.py to short-circuit upon finding the first valid secret, improving startup login latency.

Co-authored-by: davidjuarezdev <[email protected]>
@google-labs-jules
Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Apr 3, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors Qobuz app secret validation to use asyncio.as_completed with explicit task management, returning as soon as the first valid secret is found and canceling the remaining tests to reduce latency and unnecessary network I/O.

Sequence diagram for Qobuz secret validation with asyncio.as_completed

sequenceDiagram
    actor User
    participant QobuzClient
    participant asyncio
    participant QobuzAPI

    User->>QobuzClient: login()
    QobuzClient->>QobuzClient: _get_valid_secret(secrets)
    QobuzClient->>QobuzClient: create_task(_test_secret(secret_i)) for each secret
    QobuzClient->>asyncio: as_completed(tasks)

    loop for each future in as_completed
        asyncio-->>QobuzClient: next completed future
        QobuzClient->>QobuzClient: await future
        QobuzClient->>QobuzAPI: _test_secret(secret)
        QobuzAPI-->>QobuzClient: valid or invalid
        alt secret is valid
            QobuzClient-->>User: return first_valid_secret
            QobuzClient->>QobuzClient: cancel remaining pending tasks and break loop
        else secret is invalid
            QobuzClient->>QobuzClient: continue loop
        end
    end

    alt no secret is valid
        QobuzClient-->>User: raise InvalidAppSecretError
        QobuzClient->>QobuzClient: cancel remaining pending tasks
    end
Loading

Class diagram for updated QobuzClient secret validation methods

classDiagram
    class QobuzClient {
        +_get_valid_secret(secrets list~str~) str
        +_test_secret(secret str) str
    }

    class InvalidAppSecretError {
        +InvalidAppSecretError(secrets list~str~)
    }

    class asyncio {
        +create_task(coro) Task
        +as_completed(tasks list~Task~) AsyncIterator
    }

    class Task {
        +cancel() void
        +done() bool
    }

    QobuzClient ..> InvalidAppSecretError : raises
    QobuzClient ..> asyncio : uses
    asyncio --> Task : creates
    QobuzClient ..> Task : manages
Loading

File-Level Changes

Change Details Files
Short-circuit Qobuz secret validation to return on the first valid secret and cancel remaining checks.
  • Replaced asyncio.gather-based parallel secret validation with an asyncio.as_completed loop over explicitly created tasks.
  • Introduced early return upon encountering the first non-None secret validation result instead of aggregating all results.
  • Added a try/finally block to ensure all remaining tasks are cancelled to avoid background task leakage when a valid secret is found or an error is raised.
  • Adjusted error path to raise InvalidAppSecretError when no valid secret is encountered during iteration.
streamrip/client/qobuz.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 3, 2026

📝 Walkthrough

Summary by CodeRabbit

  • Refactor
    • Optimized credential validation for faster application startup and improved responsiveness. The system now returns immediately when a valid credential is found instead of completing all validation attempts. This significantly reduces initialization time. Improved resource management also ensures pending validation tasks are properly cleaned up to prevent potential resource leaks.

Walkthrough

The _get_valid_secret() method in the Qobuz client is refactored to use asyncio.as_completed() instead of asyncio.gather(), allowing immediate return when the first valid secret is found rather than waiting for all coroutines to complete. A finally block ensures pending tasks are cancelled after returning or on error.

Changes

Cohort / File(s) Summary
Async Secret Retrieval Optimization
streamrip/client/qobuz.py
Modified _get_valid_secret() to iterate through coroutines via asyncio.as_completed() for early termination upon finding first valid secret. Added finally block to cancel remaining pending tasks.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~40 minutes

Poem

🐰 One secret sought, no need to wait,
Tasks complete and seal their fate,
The first one found, we hop away,
With cleanup tucked in finally's play! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: optimizing Qobuz secret validation with short-circuit logic using asyncio improvements.
Description check ✅ Passed The description is directly related to the changeset, explaining what changed (asyncio.gather to asyncio.as_completed), why (faster validation and cancellation of remaining tasks), and the expected impact on performance.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bolt-qobuz-short-circuit-secrets-1032822583719394639
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch bolt-qobuz-short-circuit-secrets-1032822583719394639

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@streamrip/client/qobuz.py`:
- Around line 422-426: The finally block in _get_valid_secret() currently calls
task.cancel() on items in tasks but doesn't await them, so cancelled
_test_secret() coroutines may keep running against the shared aiohttp session;
update the cleanup to collect the pending tasks, call cancel() on each, then
await their completion (e.g., via asyncio.gather(pending_tasks,
return_exceptions=True)) to drain/unwind the coroutines before returning,
referencing the tasks list, _test_secret(), and _get_valid_secret() to locate
where to add the await.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Repository UI (inherited), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: c33b6938-6508-43cf-b300-1a9fd7ae34bf

📥 Commits

Reviewing files that changed from the base of the PR and between 6fb4162 and abfb3f9.

📒 Files selected for processing (1)
  • streamrip/client/qobuz.py
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: cubic · AI code reviewer
🧰 Additional context used
📓 Path-based instructions (1)
streamrip/**/*.py

📄 CodeRabbit inference engine (CLAUDE.md)

streamrip/**/*.py: Use Black-compatible code formatting with double quotes and spaces via ruff format
Lint Python code with ruff using rules: E4, E7, E9, F, I, ASYNC, N, RUF, ERA001
Use async/await for asynchronous operations instead of blocking I/O
Implement Windows compatibility by using WindowsSelectorEventLoopPolicy on Windows and the pick library instead of simple-term-menu

Files:

  • streamrip/client/qobuz.py
🔇 Additional comments (1)
streamrip/client/qobuz.py (1)

417-420: Confirm whether the secret priority should intentionally be ignored.

The code explicitly reorders secrets to prioritize the second seed/timezone pair (via move_to_end), but asyncio.as_completed() returns the first valid probe to finish, not the first in the configured order. When multiple secrets can pass _test_secret(), selection becomes timing-dependent rather than respecting the configured priority.

If this change is intentional, document the rationale. If not, preserve the original order by retaining asyncio.gather() or by checking results in the original order.

Comment thread streamrip/client/qobuz.py
Comment on lines +422 to +426
finally:
# Cancel remaining tasks to prevent background task leakage
for task in tasks:
if not task.done():
task.cancel()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

rg -n -C3 'asyncio.as_completed|task.cancel|_test_secret' streamrip/client/qobuz.py

python - <<'PY'
import asyncio

async def worker():
    try:
        await asyncio.sleep(10)
    finally:
        await asyncio.sleep(0.05)

async def main():
    tasks = [asyncio.create_task(worker()) for _ in range(2)]
    await asyncio.sleep(0)
    for task in tasks:
        task.cancel()
    print("after cancel:", [task.done() for task in tasks])
    await asyncio.sleep(0)
    print("next loop turn:", [task.done() for task in tasks])
    await asyncio.gather(*tasks, return_exceptions=True)
    print("after gather:", [task.done() for task in tasks])

asyncio.run(main())
PY

Repository: davidjuarezdev/streamrip_RipDL

Length of output: 1344


Drain cancelled tasks before returning.

task.cancel() only requests cancellation. Without awaiting the pending tasks, the cancelled _test_secret() coroutines continue unwinding against the shared aiohttp session after _get_valid_secret() returns, causing background task leakage.

Collect pending tasks, cancel them, and await their completion:

Suggested fix
         finally:
-            # Cancel remaining tasks to prevent background task leakage
-            for task in tasks:
-                if not task.done():
-                    task.cancel()
+            pending = [task for task in tasks if not task.done()]
+            for task in pending:
+                task.cancel()
+            if pending:
+                await asyncio.gather(*pending, return_exceptions=True)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@streamrip/client/qobuz.py` around lines 422 - 426, The finally block in
_get_valid_secret() currently calls task.cancel() on items in tasks but doesn't
await them, so cancelled _test_secret() coroutines may keep running against the
shared aiohttp session; update the cleanup to collect the pending tasks, call
cancel() on each, then await their completion (e.g., via
asyncio.gather(pending_tasks, return_exceptions=True)) to drain/unwind the
coroutines before returning, referencing the tasks list, _test_secret(), and
_get_valid_secret() to locate where to add the await.

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 1 file

Confidence score: 3/5

  • There is a concrete async cleanup issue in streamrip/client/qobuz.py: cancelled tasks aren’t awaited, which can leave aiohttp connections open and emit 'Task was destroyed but it is pending!' warnings.
  • This introduces some runtime risk around resource cleanup, so the change isn’t a no-brainer despite being localized.
  • Pay close attention to streamrip/client/qobuz.py - ensure cancelled tasks are awaited to close connections cleanly.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="streamrip/client/qobuz.py">

<violation number="1" location="streamrip/client/qobuz.py:424">
P2: Cancelled tasks must be awaited to avoid 'Task was destroyed but it is pending!' warnings and to ensure aiohttp connections are properly closed. Add `await asyncio.gather(*tasks, return_exceptions=True)` after cancelling.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review, or fix all with cubic.

Comment thread streamrip/client/qobuz.py
Comment on lines +424 to +426
for task in tasks:
if not task.done():
task.cancel()
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 must be awaited to avoid 'Task was destroyed but it is pending!' warnings and to ensure aiohttp connections are properly closed. Add await asyncio.gather(*tasks, return_exceptions=True) after cancelling.

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 424:

<comment>Cancelled tasks must be awaited to avoid 'Task was destroyed but it is pending!' warnings and to ensure aiohttp connections are properly closed. Add `await asyncio.gather(*tasks, return_exceptions=True)` after cancelling.</comment>

<file context>
@@ -409,14 +409,21 @@ async def _test_secret(self, secret: str) -> Optional[str]:
-        return working_secrets[0]
+        finally:
+            # Cancel remaining tasks to prevent background task leakage
+            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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant