"""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 == []