diff --git a/CHANGELOG.md b/CHANGELOG.md index 237b1e58..974523da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,7 +49,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- Fixed manual stream selection from the Stats page not enforcing M3U profile connection limits in multi-worker deployments. When a non-owning worker handled the `change_stream` request it correctly packaged `stream_id` and `m3u_profile_id` into the Redis pubsub message, but the owning worker's pubsub handler only consumed `url` and `user_agent` silently dropping both IDs before calling `stream_manager.update_url()`. Because `update_url` only calls `update_stream_profile()` when a `stream_id` is provided, the `profile_connections` counter was never updated after the switch, causing subsequent capacity checks to see incorrect counts and bypass the full-profile guard. The handler now extracts `stream_id` and `m3u_profile_id` from the event and forwards them to `update_url()`. +- Fixed manual stream selection from the Stats page not enforcing M3U profile connection limits in multi-worker deployments. When a non-owning worker handled the `change_stream` request it correctly packaged `stream_id` and `m3u_profile_id` into the Redis pubsub message, but the owning worker's pubsub handler only consumed `url` and `user_agent` silently dropping both IDs before calling `stream_manager.update_url()`. Because `update_url` only calls `update_stream_profile()` when a `stream_id` is provided, the `profile_connections` counter was never updated after the switch, causing subsequent capacity checks to see incorrect counts and bypass the full-profile guard. The handler now extracts `stream_id` and `m3u_profile_id` from the event and forwards them to `update_url()`. The bug did not affect single-worker / dev-mode deployments because the owning worker handles those requests directly without pubsub. +- Fixed the `next_stream` rotation endpoint applying the same class of bug: `get_stream_info_for_switch()` was called and returned `m3u_profile_id`, but the result was dropped when forwarding to `ChannelService.change_stream_url()`, so `update_stream_profile()` was never called and `profile_connections` counters were not updated after an automatic stream rotation. +- Fixed stream switch metadata (`url`, `user_agent`, `stream_id`, `m3u_profile`) being written to Redis before the switch was confirmed to succeed. If the switch failed, URL unchanged or exception during teardown, Redis described a URL not actually in use. Metadata is now written only after `update_url()` returns `True`; on failure the owner writes `stream_manager.url` back as the ground truth. The non-owner no longer pre-writes metadata at all, all needed info is carried in the pubsub payload and written by the owner after confirmation. - Fixed uploading a local M3U file with no expiration date set sending the string `"null"` as the `exp_date` field in the `FormData` request, causing a 400 validation error from the API. Null/undefined values are now skipped when building the `FormData` body, matching the behaviour already present in the update path. - Fixed `PATCH /api/channels/channels/edit/bulk/` returning a 500 error when the request body included a `streams` list. The bulk edit handler was iterating `validated_data` directly and calling `setattr(channel, "streams", value)`, which Django prohibits on ManyToMany fields. Also added an `@extend_schema` decorator so the Swagger UI correctly documents the endpoint as accepting a JSON array and shows the `streams` field. (Fixes #883) - Fixed several incorrect or incomplete OpenAPI (`@extend_schema`) schemas across the API: diff --git a/apps/proxy/ts_proxy/server.py b/apps/proxy/ts_proxy/server.py index 1e87ea62..b5820ca7 100644 --- a/apps/proxy/ts_proxy/server.py +++ b/apps/proxy/ts_proxy/server.py @@ -231,14 +231,8 @@ class ProxyServer: event_m3u_profile_id = data.get("m3u_profile_id") if new_url and channel_id in self.stream_managers: - # Update metadata in Redis + # Mark the switch as in-progress in Redis so other workers know to wait if self.redis_client: - metadata_key = RedisKeys.channel_metadata(channel_id) - self.redis_client.hset(metadata_key, "url", new_url) - if user_agent: - self.redis_client.hset(metadata_key, "user_agent", user_agent) - - # Set switch status status_key = RedisKeys.switch_status(channel_id) self.redis_client.set(status_key, "switching") @@ -249,6 +243,13 @@ class ProxyServer: if success: logger.info(f"Stream switch initiated for channel {channel_id}") + # Confirm the URL in metadata now that the switch happened + if self.redis_client: + metadata_key = RedisKeys.channel_metadata(channel_id) + self.redis_client.hset(metadata_key, "url", new_url) + if user_agent: + self.redis_client.hset(metadata_key, "user_agent", user_agent) + # Publish confirmation switch_result = { "event": EventType.STREAM_SWITCHED, # Use constant instead of string @@ -268,6 +269,14 @@ class ProxyServer: else: logger.error(f"Failed to switch stream for channel {channel_id}") + # Roll back the URL in metadata to what the manager will + # actually reconnect to. The non-owner may have pre-written + # the desired URL; use stream_manager.url (the ground truth) + # so Redis is consistent with the live stream. + if self.redis_client: + metadata_key = RedisKeys.channel_metadata(channel_id) + self.redis_client.hset(metadata_key, "url", stream_manager.url) + # Publish failure switch_result = { "event": EventType.STREAM_SWITCHED, diff --git a/apps/proxy/ts_proxy/services/channel_service.py b/apps/proxy/ts_proxy/services/channel_service.py index d0478e8f..ff6eb416 100644 --- a/apps/proxy/ts_proxy/services/channel_service.py +++ b/apps/proxy/ts_proxy/services/channel_service.py @@ -168,15 +168,6 @@ class ChannelService: else: result = {'status': 'success'} - # Update metadata in Redis regardless of ownership - if proxy_server.redis_client: - try: - ChannelService._update_channel_metadata(channel_id, new_url, user_agent, stream_id, m3u_profile_id) - result['metadata_updated'] = True - except Exception as e: - logger.error(f"Error updating Redis metadata: {e}", exc_info=True) - result['metadata_updated'] = False - # If we're the owner, update directly if proxy_server.am_i_owner(channel_id) and channel_id in proxy_server.stream_managers: logger.info(f"This worker is the owner, changing stream URL for channel {channel_id}") @@ -187,14 +178,33 @@ class ChannelService: success = manager.update_url(new_url, stream_id, m3u_profile_id) logger.info(f"Stream URL changed from {old_url} to {new_url}, result: {success}") + # Update Redis metadata based on the actual outcome. + # On success, write the new values. On failure, restore whatever URL + # the manager will actually reconnect to (may be old_url if the + # exception happened before self.url was reassigned, or new_url if it + # happened after) so Redis never describes a URL that isn't in use. + if proxy_server.redis_client: + try: + if success: + ChannelService._update_channel_metadata(channel_id, new_url, user_agent, stream_id, m3u_profile_id) + else: + ChannelService._update_channel_metadata(channel_id, manager.url, user_agent) + result['metadata_updated'] = True + except Exception as e: + logger.error(f"Error updating Redis metadata: {e}", exc_info=True) + result['metadata_updated'] = False + result.update({ 'direct_update': True, 'success': success, 'worker_id': proxy_server.worker_id }) else: - # If we're not the owner, publish an event for the owner to pick up - logger.info(f"Not the owner, requesting URL change via Redis PubSub") + # Not the owner: publish the switch event. The owner will update metadata + # after the actual switch attempt succeeds (or roll back on failure). + # All needed info (url, user_agent, stream_id, m3u_profile_id) is carried + # in the pubsub message, so there is no reason to pre-write metadata here. + logger.debug(f"This worker is not the owner, publishing stream switch event for channel {channel_id}") if proxy_server.redis_client: ChannelService._publish_stream_switch_event(channel_id, new_url, user_agent, stream_id, m3u_profile_id) result.update({