:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(temporal): treat fd-exhaustion on our own DB connect as transient (#102811)
Tom Owers19153a9abb0c
diff --git a/posthog/temporal/common/db_errors.py b/posthog/temporal/common/db_errors.pyindex 688d53cf2221..e89058060647 100644--- a/posthog/temporal/common/db_errors.py+++ b/posthog/temporal/common/db_errors.py@@ -1,3 +1,5 @@+import errno+from django.db import InterfaceError, InternalError, OperationalError# Substrings identifying transient Postgres failures. pgbouncer kills queries that wait too long@@ -50,7 +52,22 @@ _TRANSIENT_SQLSTATE_PREFIXES = ("57P",)_TRANSIENT_SQLSTATES = ("25006",)+def _is_too_many_open_files_error(error: BaseException) -> bool:+ """True if opening a new app-DB connection failed because this worker is out of file descriptors.++ The connect path's socket/selector setup raises a bare `OSError` — not a Django/psycopg+ exception — when `socket()` hits EMFILE (this process's fd table is full) or ENFILE (the+ system-wide table is full), before libpq has anything to wrap into `OperationalError`. Same+ transient fd-pressure condition already classified this way for a source's own connect path+ (`postgres.py::_is_too_many_open_files_error`) and for `cdp_producer.py`'s own-DB check: a+ descriptor frees the moment another connection/handle in this worker closes.+ """+ return isinstance(error, OSError) and error.errno in (errno.EMFILE, errno.ENFILE)++def is_transient_db_error(error: BaseException) -> bool:+ if _is_too_many_open_files_error(error):+ return Trueif not isinstance(error, OperationalError | InterfaceError | InternalError):return Falsesqlstate = getattr(error.__cause__, "sqlstate", None)diff --git a/posthog/temporal/common/test_db_errors.py b/posthog/temporal/common/test_db_errors.pyindex 750580df7ccc..d145b3eab050 100644--- a/posthog/temporal/common/test_db_errors.py+++ b/posthog/temporal/common/test_db_errors.py@@ -1,3 +1,5 @@+import errno+import pytestfrom django.db import InterfaceError, InternalError, OperationalError@@ -36,6 +38,14 @@ class _WithSqlstate(Exception):),(OperationalError("connection failed: FATAL: password authentication failed for user"), False),(OperationalError("no such database"), False),+ # The connect path's socket/selector setup raises a bare OSError, not an OperationalError,+ # when this worker's own fd table is full — same condition already retried on a source's+ # connect path (postgres.py::_is_too_many_open_files_error).+ (OSError(errno.EMFILE, "Too many open files"), True),+ (OSError(errno.ENFILE, "Too many open files in system"), True),+ # An unrelated errno (e.g. a real permissions problem) must not be swept up just because+ # it shares the exception type.+ (OSError(errno.EACCES, "Permission denied"), False),],)def test_is_transient_db_error_by_message(error: BaseException, expected: bool) -> None: