diff --git a/CLAUDE.md b/CLAUDE.md index 487c6eb..ca82644 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -401,8 +401,12 @@ persistence) → GUI (Qt widgets that read the manager and connect to its signal 1.8 s, a 33⅓ record; letting go is normal speed at once). The mixer is constant-power crossfade × master (the volume slider through Qt's curve). `dj/output.py` feeds a QAudioSink from **its own QThread** (100 ms - buffered), so GUI painting can't starve it; it must stop before Qt quits - (`aboutToQuit`, `closeEvent`), and **tests stub it** (conftest + buffered), so GUI painting can't starve it; it must end before Qt quits + (`shutdown()`: `aboutToQuit`, `closeEvent`). Closing DJ mode is only + `stop()` — **the thread is started once and kept** (Round 104): Qt's + PipeWire backend leaks ~24 fds per thread that ends, so a thread per + opening hit the 1024-fd limit and segfaulted in `pw_stream_connect`. + **Tests stub it** (conftest `_no_dj_sound`) — the decks would otherwise play out loud. Sync ("Sync to other track", per deck): tempo to the other deck's (the nearest octave); if both are playing, then the shortest beat nudge (`phase_shift`, never diff --git a/lintunes/__init__.py b/lintunes/__init__.py index ffdc20b..8f82ea3 100644 --- a/lintunes/__init__.py +++ b/lintunes/__init__.py @@ -1,3 +1,3 @@ """LinTunes — iTunes-style music library manager and player for Linux.""" -__version__ = "0.73.0" +__version__ = "0.73.1" diff --git a/lintunes/dj/output.py b/lintunes/dj/output.py index be46fdb..3325ce4 100644 --- a/lintunes/dj/output.py +++ b/lintunes/dj/output.py @@ -37,8 +37,9 @@ class _Pump(QObject): # Follow the system's output: a sink is opened on one device, and # when that goes (headphones in, Bluetooth on) Qt doesn't move it — # it just stops, and the decks went silent until DJ mode reopened. - self.devices = QMediaDevices() - self.devices.audioOutputsChanged.connect(self._reopen) + if getattr(self, "devices", None) is None: + self.devices = QMediaDevices() + self.devices.audioOutputsChanged.connect(self._outputs_changed) self.timer = QTimer() self.timer.setInterval(TICK_MS) self.timer.timeout.connect(self._fill) @@ -71,6 +72,11 @@ class _Pump(QObject): self._close() self._open() + @pyqtSlot() + def _outputs_changed(self): + if self.timer is not None: # stopped: nothing to move + self._reopen() + def _fill(self): if self.sink is None or self.io is None: # No device a moment ago; try again about once a second. @@ -126,7 +132,7 @@ class DjOutput(QObject): from PyQt6.QtCore import QCoreApplication app = QCoreApplication.instance() if app is not None: - app.aboutToQuit.connect(self.stop) + app.aboutToQuit.connect(self.shutdown) @property def latency_frames(self) -> int: @@ -136,13 +142,23 @@ class DjOutput(QObject): if self._running: return self._running = True - self._thread.start() + if not self._thread.isRunning(): + self._thread.start() self._go.emit() def stop(self): + """Silence: the sink closes, the thread stays for the next start. + Qt's PipeWire backend never frees what it set up in a thread that + ends — ~24 file descriptors a thread — so one per DJ-mode opening + ran the app out of them (limit 1024) and the sink's start crashed.""" if not self._running: return self._running = False self._halt.emit() - self._thread.quit() - self._thread.wait(2000) + + def shutdown(self): + """The app is ending: the thread ends too.""" + self.stop() + if self._thread.isRunning(): + self._thread.quit() + self._thread.wait(2000) diff --git a/lintunes/gui/dj_shelf.py b/lintunes/gui/dj_shelf.py index 1c462fd..dc41915 100644 --- a/lintunes/gui/dj_shelf.py +++ b/lintunes/gui/dj_shelf.py @@ -1577,6 +1577,12 @@ class DjShelf(QWidget): for v in self.views: self._refresh_deck(v) + def shutdown(self): + """The window is closing: stopped, and the audio thread ends.""" + self.stop() + if self._output is not None: + self._output.shutdown() + def set_visualizer(self, visualizer): """Feed the mix to the transport's visualizer (None: stop).""" if self._visualizer is not None and visualizer is None: diff --git a/lintunes/gui/main_window.py b/lintunes/gui/main_window.py index 5f8f14e..eef2f50 100644 --- a/lintunes/gui/main_window.py +++ b/lintunes/gui/main_window.py @@ -2940,7 +2940,7 @@ class MainWindow(QMainWindow): self._cast.shutdown() self._airplay.shutdown() if self._dj is not None: - self._dj.stop() # its audio thread must end before Qt does + self._dj.shutdown() # its audio thread must end before Qt does self.player.shutdown() self.player.loudness.save() self._manager.flush() diff --git a/tests/conftest.py b/tests/conftest.py index e82b4b1..e4ce312 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -33,6 +33,7 @@ def _no_dj_sound(monkeypatch): from lintunes.dj import output monkeypatch.setattr(output.DjOutput, "start", lambda self: None) monkeypatch.setattr(output.DjOutput, "stop", lambda self: None) + monkeypatch.setattr(output.DjOutput, "shutdown", lambda self: None) @pytest.fixture(autouse=True) diff --git a/tests/test_round104.py b/tests/test_round104.py new file mode 100644 index 0000000..92590a4 --- /dev/null +++ b/tests/test_round104.py @@ -0,0 +1,75 @@ +"""Round 104: opening DJ mode crashed the app after enough openings. + +Each opening started a fresh audio thread, and Qt's PipeWire backend never +frees what it set up in a thread that ends (~24 file descriptors each). With +the desktop's limit of 1024 the sink's start failed with "Too many open +files" and segfaulted inside pw_stream_connect. The thread now lives as long +as the output; stop() only silences it. +""" + +from PyQt6.QtCore import QCoreApplication + +from lintunes.dj import output + + +class _QuietPump(output._Pump): + """The real pump's thread handling, without opening a sound device.""" + + starts = 0 + + def start(self): + _QuietPump.starts += 1 + self.timer = object() + + def stop(self): + self.timer = None + + +def _real_output(monkeypatch): + # conftest stubs these out so no test plays sound; the pump is stubbed + # here instead. + monkeypatch.setattr(output.DjOutput, "start", _ORIG_START) + monkeypatch.setattr(output.DjOutput, "stop", _ORIG_STOP) + monkeypatch.setattr(output.DjOutput, "shutdown", _ORIG_SHUTDOWN) + monkeypatch.setattr(output, "_Pump", _QuietPump) + _QuietPump.starts = 0 + + +_ORIG_START = output.DjOutput.start +_ORIG_STOP = output.DjOutput.stop +_ORIG_SHUTDOWN = output.DjOutput.shutdown + + +def _wait_for(cond): + for _ in range(200): + QCoreApplication.processEvents() + if cond(): + return True + output.QThread.msleep(5) + return False + + +def test_reopening_keeps_one_audio_thread(qapp, monkeypatch): + _real_output(monkeypatch) + out = output.DjOutput(mixer=None) + started = [] + out._thread.started.connect(lambda: started.append(1)) + try: + for i in range(5): + out.start() + assert _wait_for(lambda: _QuietPump.starts == i + 1) + out.stop() + assert out._thread.isRunning() + assert _wait_for(lambda: len(started) == 1) + assert len(started) == 1 + finally: + out.shutdown() + assert not out._thread.isRunning() + + +def test_device_change_while_stopped_opens_nothing(qapp, monkeypatch): + pump = output._Pump(mixer=None) + opened = [] + monkeypatch.setattr(pump, "_open", lambda: opened.append(1) or True) + pump._outputs_changed() # DJ mode closed: timer is None + assert opened == []