Repository navigation
nightly-b8711d88-ls2
Pre-releaseCI
LinuxServer changes
No changes
Upstream changes
Source: beetbox/beets@b8711d8
replaygain: stop exception watcher from busy-waiting (#7112)
replaygain: stop exception watcher from busy-waiting
Description
Fixes #5449.
I had the same bug with my music library. No problems when importing by
album,
but an infinite loop using 100% CPU when scanning the music library.
When the replaygain thread pool is open (threads > 0, the default),
ExceptionWatcher.run() polls its queue with get_nowait() in a loop.
That thread holds a CPU core at 100%. It's mono threaded pyhton so the
GIL, starves
the workers. On a my library beet replaygain can spin for
hours without starting a single analysis, which matches the "100% CPU
for
hours, the replaygain command does not run" comment in #5449.
The fix use get(timeout=0.1) instead, so the watcher sleeps until an
exception arrives and still notices _stopevent promptly on join().
Measured on my library (17,310 tracks that already have ReplayGain
values on most
tracks), ffmpeg backend, -t 4:
| 132 tracks | full library | |
|---|---|---|
| before | 4.7 s | still running after 4 hours |
| after | 0.5 s | 8.4 s |
--threads 0 was a working workaround because it skips the pool and the
watcher
altogether. Setting threads: 0 in the config does not work (it reaches
ThreadPool(0) and raises ValueError); that is a separate issue.
Tests: test_exception_watcher_does_not_busy_wait counts calls to the
queue's
get methods during 0.5 s. It fails on the current code (about 630,000
calls).
It's the point of the fix (a handful). A second test checks that an
exception
put on the queue still triggers the callback and is re-raised.
AI disclosure: I used Claude Code to profile the problem, write the
patch,
the tests. I reviewed the change, ran the tests and the benchmark above.