From ee26294106141070cde1ade35a429925842b43ce Mon Sep 17 00:00:00 2001 From: ComputerTech Date: Tue, 20 Jan 2026 17:49:00 +0000 Subject: [PATCH] Fix critical bugs: broadcast thread, resource cleanup, and Socket.IO error handling --- techdj_qt.py | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/techdj_qt.py b/techdj_qt.py index 985448d..ff7ba26 100644 --- a/techdj_qt.py +++ b/techdj_qt.py @@ -380,6 +380,8 @@ class BroadcastThread(QThread): output_thread = threading.Thread(target=read_output, daemon=True) output_thread.start() + print(f"📡 FFmpeg broadcast process started ({self.bitrate})") + # Worker to feed stdin from the broadcast queue while self.running: try: @@ -399,8 +401,6 @@ class BroadcastThread(QThread): print(f"Broadcast input error: {e}") break - print(f"📡 FFmpeg broadcast process started ({self.bitrate})") - except Exception as e: self.error.emit(str(e)) self.running = False @@ -415,6 +415,8 @@ class BroadcastThread(QThread): except: self.process.kill() self.process = None + # Give output thread time to finish + time.sleep(0.1) print("🛑 Broadcast process stopped") @@ -1948,8 +1950,11 @@ class TechDJMainWindow(QMainWindow): QMessageBox.warning(self, "Broadcast Error", f"Could not start broadcast:\n{e}") else: # Stop broadcast - if self.socket: - self.socket.emit('stop_broadcast') + if self.socket and self.socket.connected: + try: + self.socket.emit('stop_broadcast') + except Exception as e: + print(f"❌ Failed to emit stop_broadcast: {e}") self.audio_engine.is_broadcasting = False if self.broadcast_thread: @@ -1979,8 +1984,11 @@ class TechDJMainWindow(QMainWindow): def on_broadcast_chunk(self, chunk): """Send encoded chunk to server via Socket.IO""" - if self.socket and self.broadcasting: - self.socket.emit('audio_chunk', chunk) + if self.socket and self.socket.connected and self.broadcasting: + try: + self.socket.emit('audio_chunk', chunk) + except Exception as e: + print(f"❌ Failed to send chunk: {e}") def on_listener_count(self, data): """Update listener count from server""" @@ -2073,7 +2081,27 @@ class TechDJMainWindow(QMainWindow): self.settings_panel.move(self.width() - 420, 20) def closeEvent(self, event): + """Clean up resources before closing""" + # Stop broadcast if active + if self.broadcasting: + self.toggle_broadcast() + + # Disconnect Socket.IO + if self.socket and self.socket.connected: + try: + self.socket.disconnect() + print("🔌 Socket.IO disconnected") + except Exception as e: + print(f"⚠️ Error disconnecting Socket.IO: {e}") + + # Stop audio engine self.audio_engine.stop_stream() + + # Wait for download threads to finish + for filename, thread in list(self.download_threads.items()): + if thread.isRunning(): + thread.wait(1000) # Wait up to 1 second + event.accept()