Skip to content

Stop the audio analysis background scan from spawning a task per track - #6384

Merged
marcelveldt merged 3 commits into
music-assistant:devfrom
balloobbot:fix/background-scan-worker-pool
Sep 19, 2026
Merged

marcelveldt merged 3 commits into
music-assistant:devfrom
balloobbot:fix/background-scan-worker-pool

Conversation

@balloobbot

@balloobbot balloobbot commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

What does this implement/fix?

This PR is best reviewed ignoring white space.

The nightly background audio-analysis scan created one asyncio task per candidate track up front, before any analysis started. On a large library that parks thousands of idle tasks for the whole run (one live install held 7,914 on a 10,709-track library), adding to event-loop pressure.

Workers now pull from a shared iterator, so only the configured concurrency runs at a time, no matter how big the library is. Deferred tracks are counted from what the workers never reached. The run budget, provider-availability skips, the processed counter and the existing log lines are unchanged.

Changes

  • Replace the per-candidate task fan-out with a fixed pool of workers pulling from a shared iterator.
  • Derive the deferred count from the candidates the workers never pulled.
  • Add a test that asserts the scan's task count tracks the configured concurrency, not the library size.

Related issue (if applicable):

Types of changes

  • Bugfix (non-breaking change which fixes an issue) — bugfix
  • New feature (non-breaking change which adds functionality) — new-feature
  • Enhancement to an existing feature — enhancement
  • New music/player/metadata/plugin provider — new-provider
  • Breaking change (fix or feature that would cause existing functionality to not work as expected) — breaking-change
  • Refactor (no behaviour change) — refactor
  • Documentation only — documentation
  • Maintenance / chore — maintenance
  • CI / workflow change — ci
  • Dependencies bump — dependencies

Checklist

  • The code change is tested and works locally.
  • pre-commit run --all-files passes.
  • pytest passes, and tests have been added/updated under tests/ where applicable.
  • For changes to shared models, the companion PR in music-assistant/models is linked.
  • For changes affecting the UI, the companion PR in music-assistant/frontend is linked.
  • I have read and complied with the project's AI Policy for any AI-assisted contributions.
  • I have raised a PR against the documentation repository targeting the main or beta branch as appropriate.

@musicassistant-bot

musicassistant-bot Bot commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

✅ The title and description are good to go. Thanks!

@balloob
balloob marked this pull request as ready for review September 17, 2026 16:14
Copilot AI balanced review requested due to automatic review settings September 17, 2026 16:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The worker-count regression needs a targeted automated test.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Bounds background audio-analysis scans to fixed worker concurrency, preventing task counts from scaling with library size.

Changes:

  • Replaces per-candidate tasks and semaphore with iterator-driven workers.
  • Counts unclaimed candidates as deferred.
File summaries
File Description
music_assistant/controllers/streams/audio_analysis.py Implements the bounded worker pool and deferred counting.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Balanced

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread music_assistant/controllers/streams/audio_analysis.py
balloob and others added 3 commits September 19, 2026 14:17
_run_background_scan fetched every candidate (limit=0) and handed the
whole list to asyncio.gather, so one Task was created per library track
before any analysis began. A profiler capture from a 10,709-track
install showed 7,914 tasks parked on the semaphore for the whole run,
out of 8,005 in the process. The semaphore bounded the concurrent work
but not the task objects, and the cost grows linearly with library size.

Spawn `concurrency` workers that pull from a shared iterator instead.
Only that many tasks exist at a time, which makes the semaphore
redundant. Candidates left unpulled when the run budget expires are
counted as deferred, as before.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01M3jXPrteRvvgAPD7xvXcFH
It narrated the change rather than the code as it stands.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01M3jXPrteRvvgAPD7xvXcFH
@marcelveldt
marcelveldt force-pushed the fix/background-scan-worker-pool branch from dff9a13 to bdf4bef Compare September 19, 2026 12:24
@marcelveldt marcelveldt changed the title Bound the background analysis scan to a worker pool Stop the audio analysis background scan from spawning a task per track Sep 19, 2026
@marcelveldt
marcelveldt marked this pull request as draft September 19, 2026 12:25
@marcelveldt
marcelveldt requested a balanced review from Copilot September 19, 2026 12:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The implementation preserves existing scan behavior while directly fixing and testing unbounded task creation.

Review effort: Balanced
Findings: 1 Low severity

Open (1)

@marcelveldt
marcelveldt marked this pull request as ready for review September 19, 2026 12:36
@marcelveldt
marcelveldt merged commit fe83139 into music-assistant:dev Sep 19, 2026
19 checks passed
github-actions Bot pushed a commit that referenced this pull request Sep 19, 2026
#6384)

The nightly background audio-analysis scan created one asyncio task per candidate track up front. On large libraries that parked thousands of idle tasks for the whole run. Workers now pull from a shared iterator, so only the configured concurrency runs at a time.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants