v0.73.1: opening DJ mode no longer crashes after enough openings

Each opening started a new audio thread, and Qt's PipeWire backend leaks
~24 file descriptors per thread that ends. At the desktop's 1024-fd limit
QAudioSink.start() failed ("Too many open files") and segfaulted in
pw_stream_connect. The thread now lives as long as the output: stop()
only silences the pump, shutdown() (window close / aboutToQuit) ends it.
This commit is contained in:
2026-10-20 21:16:26 -07:00
parent a52542750c
commit b76a69f504
7 changed files with 112 additions and 10 deletions
+6 -2
View File
@@ -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 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). constant-power crossfade × master (the volume slider through Qt's curve).
`dj/output.py` feeds a QAudioSink from **its own QThread** (100 ms `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 buffered), so GUI painting can't starve it; it must end before Qt quits
(`aboutToQuit`, `closeEvent`), and **tests stub it** (conftest (`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 `_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); 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 if both are playing, then the shortest beat nudge (`phase_shift`, never
+1 -1
View File
@@ -1,3 +1,3 @@
"""LinTunes — iTunes-style music library manager and player for Linux.""" """LinTunes — iTunes-style music library manager and player for Linux."""
__version__ = "0.73.0" __version__ = "0.73.1"
+22 -6
View File
@@ -37,8 +37,9 @@ class _Pump(QObject):
# Follow the system's output: a sink is opened on one device, and # 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 — # when that goes (headphones in, Bluetooth on) Qt doesn't move it —
# it just stops, and the decks went silent until DJ mode reopened. # it just stops, and the decks went silent until DJ mode reopened.
self.devices = QMediaDevices() if getattr(self, "devices", None) is None:
self.devices.audioOutputsChanged.connect(self._reopen) self.devices = QMediaDevices()
self.devices.audioOutputsChanged.connect(self._outputs_changed)
self.timer = QTimer() self.timer = QTimer()
self.timer.setInterval(TICK_MS) self.timer.setInterval(TICK_MS)
self.timer.timeout.connect(self._fill) self.timer.timeout.connect(self._fill)
@@ -71,6 +72,11 @@ class _Pump(QObject):
self._close() self._close()
self._open() self._open()
@pyqtSlot()
def _outputs_changed(self):
if self.timer is not None: # stopped: nothing to move
self._reopen()
def _fill(self): def _fill(self):
if self.sink is None or self.io is None: if self.sink is None or self.io is None:
# No device a moment ago; try again about once a second. # No device a moment ago; try again about once a second.
@@ -126,7 +132,7 @@ class DjOutput(QObject):
from PyQt6.QtCore import QCoreApplication from PyQt6.QtCore import QCoreApplication
app = QCoreApplication.instance() app = QCoreApplication.instance()
if app is not None: if app is not None:
app.aboutToQuit.connect(self.stop) app.aboutToQuit.connect(self.shutdown)
@property @property
def latency_frames(self) -> int: def latency_frames(self) -> int:
@@ -136,13 +142,23 @@ class DjOutput(QObject):
if self._running: if self._running:
return return
self._running = True self._running = True
self._thread.start() if not self._thread.isRunning():
self._thread.start()
self._go.emit() self._go.emit()
def stop(self): 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: if not self._running:
return return
self._running = False self._running = False
self._halt.emit() 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)
+6
View File
@@ -1577,6 +1577,12 @@ class DjShelf(QWidget):
for v in self.views: for v in self.views:
self._refresh_deck(v) 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): def set_visualizer(self, visualizer):
"""Feed the mix to the transport's visualizer (None: stop).""" """Feed the mix to the transport's visualizer (None: stop)."""
if self._visualizer is not None and visualizer is None: if self._visualizer is not None and visualizer is None:
+1 -1
View File
@@ -2940,7 +2940,7 @@ class MainWindow(QMainWindow):
self._cast.shutdown() self._cast.shutdown()
self._airplay.shutdown() self._airplay.shutdown()
if self._dj is not None: 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.shutdown()
self.player.loudness.save() self.player.loudness.save()
self._manager.flush() self._manager.flush()
+1
View File
@@ -33,6 +33,7 @@ def _no_dj_sound(monkeypatch):
from lintunes.dj import output from lintunes.dj import output
monkeypatch.setattr(output.DjOutput, "start", lambda self: None) monkeypatch.setattr(output.DjOutput, "start", lambda self: None)
monkeypatch.setattr(output.DjOutput, "stop", lambda self: None) monkeypatch.setattr(output.DjOutput, "stop", lambda self: None)
monkeypatch.setattr(output.DjOutput, "shutdown", lambda self: None)
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
+75
View File
@@ -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 == []