From b439eb810c38244aa688e03babc940dd8bfc81c9 Mon Sep 17 00:00:00 2001 From: SergeantPanda Date: Mon, 28 Apr 2025 15:05:58 -0500 Subject: [PATCH] Cleanup channel lock instead of stream lock. --- apps/proxy/ts_proxy/stream_generator.py | 11 ++++++----- apps/proxy/ts_proxy/stream_manager.py | 19 ++++++++++--------- apps/proxy/ts_proxy/views.py | 1 - 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/apps/proxy/ts_proxy/stream_generator.py b/apps/proxy/ts_proxy/stream_generator.py index 9377a079..17e53b9d 100644 --- a/apps/proxy/ts_proxy/stream_generator.py +++ b/apps/proxy/ts_proxy/stream_generator.py @@ -380,14 +380,15 @@ class StreamGenerator: client_count = proxy_server.client_managers[self.channel_id].get_total_client_count() # Only the last client or owner should release the stream if client_count <= 1 and proxy_server.am_i_owner(self.channel_id): - from apps.channels.models import Stream + from apps.channels.models import Channel try: - stream = Stream.objects.get(pk=stream_id) - stream.release_stream() + # Get the channel by UUID + channel = Channel.objects.get(uuid=self.channel_id) + channel.release_stream() stream_released = True - logger.debug(f"[{self.client_id}] Released stream {stream_id} for channel {self.channel_id}") + logger.debug(f"[{self.client_id}] Released stream for channel {self.channel_id}") except Exception as e: - logger.error(f"[{self.client_id}] Error releasing stream {stream_id}: {e}") + logger.error(f"[{self.client_id}] Error releasing stream for channel {self.channel_id}: {e}") except Exception as e: logger.error(f"[{self.client_id}] Error checking stream data for release: {e}") diff --git a/apps/proxy/ts_proxy/stream_manager.py b/apps/proxy/ts_proxy/stream_manager.py index 2615758f..8f7b1817 100644 --- a/apps/proxy/ts_proxy/stream_manager.py +++ b/apps/proxy/ts_proxy/stream_manager.py @@ -502,15 +502,6 @@ class StreamManager: owner_key = RedisKeys.channel_owner(self.channel_id) current_owner = self.buffer.redis_client.get(owner_key) - if current_owner and current_owner.decode('utf-8') == self.worker_id: - try: - from apps.channels.models import Stream - stream = Stream.objects.get(pk=self.current_stream_id) - stream.release_stream() - logger.info(f"Released stream {self.current_stream_id} for channel {self.channel_id}") - except Exception as e: - logger.error(f"Error releasing stream {self.current_stream_id}: {e}") - # Cancel all buffer check timers for timer in list(self._buffer_check_timers): try: @@ -552,6 +543,16 @@ class StreamManager: logger.info(f"Switching stream URL from {self.url} to {new_url}") + # Release old stream resources if we have a current stream ID + if self.current_stream_id: + try: + from apps.channels.models import Stream + stream = Stream.objects.get(pk=self.current_stream_id) + stream.release_stream() + logger.info(f"Released stream {self.current_stream_id} for channel {self.channel_id}") + except Exception as e: + logger.error(f"Error releasing stream {self.current_stream_id}: {e}") + # CRITICAL: Set a flag to prevent immediate reconnection with old URL self.url_switching = True diff --git a/apps/proxy/ts_proxy/views.py b/apps/proxy/ts_proxy/views.py index 87a8e51b..222da4e3 100644 --- a/apps/proxy/ts_proxy/views.py +++ b/apps/proxy/ts_proxy/views.py @@ -261,7 +261,6 @@ def stream_ts(request, channel_id): logger.info(f"[{client_id}] Successfully initialized channel {channel_id}") channel_initializing = True - logger.info(f"[{client_id}] Channel {channel_id} initialization started") # Register client - can do this regardless of initialization state # Create local resources if needed