Skip to content

: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.

fix(postgres): retry Supavisor authenticated-state socket close in-process (#68069)

Tom Owersbd84697b81c1
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py
index 3cc9b4356f6a..2a05f6c4e34d 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/postgres.py
@@ -228,7 +228,19 @@ _CONNECTION_DROPPED_ERROR_SUBSTRINGS = (
# exhausted or every backend was busy. A slot frees the moment another session returns one.
# Both are the same transient class as the libpq drops above and recover on reconnect. Genuine
# XX000 internal errors (data corruption, etc.) carry a different code and stay non-recoverable.
-_POOLER_CONNECTION_DROPPED_ERROR_SUBSTRINGS = ("edbhandlerexited", "echeckoutretries")
+#
+# Supavisor also surfaces a backend socket that closed mid-session — after the client authenticated
+# — as "Internal error (authenticated): :closed", where ":closed" is the Erlang gen_tcp reason for a
+# peer-closed socket (its DbHandler lost the backend connection to an idle cull, restart, or
+# failover). There's no error code here, so match the full phrase including the ":closed" reason: a
+# fresh reconnect re-establishes a new session — the same transient class. Matching only the
+# "(authenticated)" wrapper would be too broad: a non-:closed "Internal error (authenticated): ..."
+# could be a permanent pooler/protocol failure that should surface immediately, not be retried.
+_POOLER_CONNECTION_DROPPED_ERROR_SUBSTRINGS = (
+ "edbhandlerexited",
+ "echeckoutretries",
+ "internal error (authenticated): :closed",
+)
# Connect-time capacity errors: the source refuses a *new* connection because it has hit a
# connection limit, not because anything is misconfigured. PostgreSQL raises "sorry, too many
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py
index 3fd698c2f6cb..9bbf51bac8a5 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py
@@ -1114,6 +1114,12 @@ class TestIsConnectionDroppedError:
# retrying internally. Same transient pooler class as EDBHANDLEREXITED; recovers on
# reconnect once a session returns a connection to the pool.
psycopg.errors.InternalError_("(ECHECKOUTRETRIES) failed to check out a connection after multiple retries"),
+ # Supavisor loses the backend socket mid-session (idle cull, restart, failover) and, once
+ # the client is past auth, surfaces it as an XX000 InternalError_ "Internal error
+ # (authenticated): :closed" — ":closed" being the Erlang gen_tcp peer-closed reason. No
+ # error code, so it's matched on the full phrase including the ":closed" reason; same
+ # transient class as the pooler drops above and recovers on reconnect.
+ psycopg.errors.InternalError_("Internal error (authenticated): :closed"),
# Supavisor reports a transient timeout reaching the upstream backend as a
# ConnectionFailure (08006, an OperationalError) carrying the Erlang-tuple reason
# "{:error, :etimedout}" — a transient drop the in-process recovery must catch.
@@ -1154,6 +1160,10 @@ class TestIsConnectionDroppedError:
# non-recoverable — the InternalError_ match is scoped to the known pooler codes
# ("(EDBHANDLEREXITED)" / "(ECHECKOUTRETRIES)"), not every XX000.
psycopg.errors.InternalError_("XX000: internal error: something went wrong"),
+ # The Supavisor authenticated-state match is scoped to the ":closed" socket-drop reason.
+ # Any other "Internal error (authenticated): ..." reason could be a permanent pooler or
+ # protocol failure that must surface immediately, so it must NOT be treated as a drop.
+ psycopg.errors.InternalError_("Internal error (authenticated): :protocol_error"),
# libpq's bare English "Connection refused" is a permanent wrong-host/port
# misconfiguration (non-retryable in source.py) and must NOT be confused with Supavisor's
# transient Erlang-tuple "{:error, :econnrefused}" — broadening the match to a plain