: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(oauth): treat connection-reset db errors as retryable on token endpoint (#65265)
posthog[bot]db4b7426c527
diff --git a/posthog/api/oauth/test_views.py b/posthog/api/oauth/test_views.pyindex 4077ff6d2a70..25f5146539cf 100644--- a/posthog/api/oauth/test_views.py+++ b/posthog/api/oauth/test_views.py@@ -1167,12 +1167,23 @@ class TestOAuthAPI(APIBaseTest):self.assertEqual(body["error"], "invalid_grant")self.assertIn("error_description", body)- def test_token_endpoint_returns_temporarily_unavailable_on_pgbouncer_query_wait_timeout(self):+ @parameterized.expand(+ [+ ("query_wait_timeout", "query_wait_timeout"),+ (+ "connection_reset",+ "server closed the connection unexpectedly\n\tThis probably means the server "+ "terminated abnormally before or while processing the request.",+ ),+ ("connection_failed", "connection failed"),+ ]+ )+ def test_token_endpoint_returns_temporarily_unavailable_on_transient_db_error(self, _name, error_message):token_data = {**self.base_token_body, "code": "does_not_matter"}with patch("oauth2_provider.views.base.TokenView.post",- side_effect=OperationalError("query_wait_timeout"),+ side_effect=OperationalError(error_message),):response = self.post("/oauth/token/", token_data)diff --git a/posthog/api/oauth/views.py b/posthog/api/oauth/views.pyindex d69f343f8ec4..9c2e13ee67ee 100644--- a/posthog/api/oauth/views.py+++ b/posthog/api/oauth/views.py@@ -92,6 +92,23 @@ def get_region_info() -> dict | None:return None+# Substrings identifying transient database failures that OAuth clients should retry.+# PgBouncer (port 6543) kills queries waiting too long for a backend connection with+# `query_wait_timeout`, and surfaces dropped/reset backend connections as connection+# failures. Both are retryable rather than permanent, so map them to a 503 instead of+# letting them escape as an unhandled 500.+_TRANSIENT_DB_ERROR_MARKERS = (+ "query_wait_timeout",+ "server closed the connection unexpectedly",+ "connection failed",+)+++def _is_transient_db_error(error: Exception) -> bool:+ message = str(error)+ return any(marker in message for marker in _TRANSIENT_DB_ERROR_MARKERS)++def _temporarily_unavailable_response(retry_after_seconds: int = 1) -> JsonResponse:"""RFC 6749 `temporarily_unavailable` response with HTTP 503 and Retry-After.@@ -1214,10 +1231,10 @@ class OAuthTokenView(TokenView):status=400,)except OperationalError as e:- # PgBouncer kills queries that wait too long for a backend connection with- # `query_wait_timeout`. The resulting OperationalError otherwise bubbles up- # as an unhandled 500 — translate it into a retryable response.- if "query_wait_timeout" not in str(e):+ # Transient database failures (PgBouncer `query_wait_timeout`, dropped/reset+ # backend connections during client authentication) otherwise bubble up as an+ # unhandled 500 — translate them into a retryable response.+ if not _is_transient_db_error(e):raiselogger.warning("oauth_token_db_pool_pressure",