From 1258e1eada2828ceccef8d1f787716aa0ff33343 Mon Sep 17 00:00:00 2001 From: Marc 'risson' Schmitt Date: Mon, 4 May 2026 15:06:30 +0200 Subject: [PATCH] lifecycle/worker_process: fix healthchecks and metrics not reloading db connections after a failure (#21992) --- lifecycle/worker_process.py | 41 +++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/lifecycle/worker_process.py b/lifecycle/worker_process.py index dddfbebd72..c37f012f92 100755 --- a/lifecycle/worker_process.py +++ b/lifecycle/worker_process.py @@ -28,20 +28,45 @@ class HttpHandler(BaseHTTPRequestHandler): _ = db_conn.cursor() def do_GET(self): - if self.path == "/-/metrics/": - from authentik.root.monitoring import monitoring_set + from django.db import ( + DatabaseError, + InterfaceError, + OperationalError, + connections, + ) + from psycopg.errors import AdminShutdown - monitoring_set.send_robust(self) - self.send_response(200) + from authentik.root.monitoring import monitoring_set + + DATABASE_ERRORS = ( + AdminShutdown, + InterfaceError, + DatabaseError, + ConnectionError, + OperationalError, + ) + + if self.path == "/-/metrics/": + try: + monitoring_set.send(self) + except DATABASE_ERRORS as exc: + LOGGER.warning("failed to send monitoring_set", exc=exc) + for db_conn in connections.all(): + db_conn.close() + self.send_response(503) + else: + self.send_response(200) self.end_headers() elif self.path == "/-/health/ready/": - from django.db.utils import OperationalError - try: self.check_db() - except OperationalError: + except DATABASE_ERRORS as exc: + LOGGER.warning("failed to check database health", exc=exc) + for db_conn in connections.all(): + db_conn.close() self.send_response(503) - self.send_response(200) + else: + self.send_response(200) self.end_headers() else: self.send_response(200)