Dispatcharr/core
PFalko 8bd38ad71c Fix stream ownership bugs causing streams to die after 30-200s
Three interrelated bugs cause TS proxy streams to terminate prematurely
in multi-worker uWSGI deployments:

1. Double ProxyServer instantiation: ProxyConfig.ready() in apps/proxy/apps.py
   calls TSProxyServer() directly, bypassing get_instance(). The subsequent
   TSProxyConfig.ready() call to get_instance() creates a second instance.
   Each instance starts its own cleanup thread, but only one holds channel
   data — the orphaned cleanup thread cannot extend ownership.

2. Redis flushdb() on every client init: RedisClient.get_client() in
   core/utils.py calls flushdb() whenever a new connection is created.
   Celery autoscale workers spawning mid-stream nuke all Redis keys
   including ownership, client records, and channel metadata.

3. No recovery from expired ownership: get_channel_owner() has a TOCTOU
   bug (two separate GET calls in a lambda). extend_ownership() silently
   fails when keys expire. Non-owner cleanup unconditionally kills streams
   even when the worker holds the stream_manager.

Fixes:
- Use TSProxyServer.get_instance() in ProxyConfig.ready()
- Remove flushdb() from Redis client initialization
- Use sentinel pattern for gevent-safe singleton (threading.Lock does not
  work with gevent greenlets)
- Single GET in get_channel_owner() to avoid TOCTOU race
- Re-acquire expired ownership keys in extend_ownership()
- Attempt re-acquisition before cleanup in non-owner path

Relates to #992, #980
2026-02-27 16:40:28 +01:00
..
fixtures feat: add system notifications and update checks 2026-02-03 09:24:02 -06:00
management/commands Refactor CoreSettings to use JSONField for value storage and update related logic for proper type handling. Adjusted serializers and forms to accommodate new data structure, ensuring seamless integration across the application. 2026-01-13 12:18:34 -06:00
migrations feat: add system notifications and update checks 2026-02-03 09:24:02 -06:00
__init__.py Alpha v3 2025-02-21 15:31:59 -06:00
admin.py modified database fields for consistency, removed custom_url from streams (no longer needed) 2025-03-16 09:07:10 -04:00
api_urls.py feat: add system notifications and update checks 2026-02-03 09:24:02 -06:00
api_views.py Fix #954: Superuser detection; Feature #1004: User account disable/enable 2026-02-22 18:05:02 -06:00
apps.py Enhancement: Refactored app initialization to prevent redundant execution across multiple worker processes. Created dispatcharr.app_initialization utility module with should_skip_initialization() function that prevents custom initialization tasks (backup scheduler sync, developer notifications sync) from running during management commands, in worker processes, or in development servers. This significantly reduces startup overhead in multi-worker deployments (e.g., uWSGI with 10 workers now syncs the scheduler once instead of 10 times). Applied to both CoreConfig and BackupsConfig apps. 2026-02-05 13:48:01 -06:00
command_utils.py Bypass redis for management commands. 2025-03-22 12:23:54 -05:00
developer_notifications.py Fix #954: Superuser detection; Feature #1004: User account disable/enable 2026-02-22 18:05:02 -06:00
models.py Merge pull request #894 from CodeBormen/Feature/771-Auto-Matching-Improvments 2026-02-03 10:20:45 -06:00
redis_pubsub.py Bypass redis for management commands. 2025-03-22 12:23:54 -05:00
scheduling.py feat(scheduling): add cron builder and refactor scheduling components (Closes #165) 2026-02-12 18:08:13 -06:00
serializers.py feat: add system notifications and update checks 2026-02-03 09:24:02 -06:00
signals.py feat: add system notifications and update checks 2026-02-03 09:24:02 -06:00
tasks.py feat: add system notifications and update checks 2026-02-03 09:24:02 -06:00
tests.py Alpha v3 2025-02-21 15:31:59 -06:00
urls.py Pre-Alpha v4 2025-02-24 15:04:03 -06:00
utils.py Fix stream ownership bugs causing streams to die after 30-200s 2026-02-27 16:40:28 +01:00
views.py feat: Add Redis authentication support for modular deployment 2026-02-03 21:42:06 -06:00
xtream_codes.py Convert refresh time to local time. 2025-09-09 16:43:57 -05:00