Dispatcharr/dispatcharr/db/backends
nagelm 49189465d2 fix(db): pin session timezone to UTC via libpq startup packet
DB sessions were never actually pinned to UTC. Three stacked failures:

1. The connection_created receiver (_force_utc0 in core/apps.py) was a
   nested closure connected with Django's default weak reference. It was
   garbage-collected as soon as CoreConfig.ready() returned, so
   SET TIME ZONE 'UTC0' fired into a dead weakref and never executed -
   in any version since it landed (verified: the signal's receivers list
   on a live 0.27.2 shows a dead weakref).

2. Sessions were UTC on older stacks anyway because Django's own
   init_connection_state configured the timezone. Since native
   psycopg-pool support (Django 5.1+), that path is gated on
   `not self.pool` - and the geventpool mixin's `pool` property is
   always truthy, so Django silently skips timezone (and role)
   configuration for this backend on every connection. This is what
   actually regressed at the psycopg2->3 / Django upgrade: the masking
   layer disappeared, not the (already dead) signal.

3. psycopg3 logs "unknown PostgreSQL timezone: 'UTC0'" because the
   POSIX spec is unresolvable in Python zoneinfo (cosmetic, but it
   means 'UTC0' buys nothing on psycopg3).

Net effect: every session ran at the server-default timezone (verified
live: SHOW TimeZone through the pool returns 'Etc/UTC'). Deployments
whose Postgres default is non-UTC (e.g. /etc/localtime bind-mounts,
the original issue 651 report) get the EPG offset corruption back.

Fix: pin the GUC in the libpq startup packet in the pool backend's
get_connection_params():

- covers every connection the gevent pool creates; no signal, GC, or
  Django-flow dependency (same pattern as the client_encoding pin the
  pool already applies)
- startup-packet GUCs are the session default: they survive ROLLBACK,
  and RESET TimeZone returns to UTC rather than the server default
- 'UTC' resolves cleanly in psycopg3's zoneinfo lookup, and PostgreSQL
  resolves it against its own bundled tzdata, not host-mounted files

The dead signal is removed. Regression tests exercise the session
timezone through the real pool backend explicitly (the test runner's
default engine is the vanilla Django backend), and assert the
RESET-returns-to-UTC session-default property.

Tested: live 0.27.2 AIO (psycopg 3.3.4, PostgreSQL 17) - unpatched
backend+pool session shows 'Etc/UTC'; patched shows 'UTC' incl. after
RESET and ROLLBACK. Full A/B against the dev image with the server
default forced to Europe/Zurich: stock renders a 12:00Z source
programme as start="20260715140000 +0000" in /output/epg; patched
renders it correctly; the regression tests fail on stock and pass
when patched.
2026-07-15 10:20:38 +12:00
..
postgresql_psycopg3 fix(db): pin session timezone to UTC via libpq startup packet 2026-07-15 10:20:38 +12:00
__init__.py feat(settings): add DATABASE_POOL_CONN_MAX_LIFETIME for connection management and update database engine path (Fixes #1343) 2026-06-12 11:31:09 -05:00