Cleanup channel lock instead of stream lock.

This commit is contained in:
SergeantPanda 2025-04-28 15:05:58 -05:00
parent 0b8f20dc22
commit b439eb810c
3 changed files with 16 additions and 15 deletions

View file

@ -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}")

View file

@ -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

View file

@ -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