: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(postgres): keep transient SSL connection drops retryable (#66653)
Tom Owers16dce7493986
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.pyindex dc88b734b123..9213cee76379 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@@ -172,6 +172,12 @@ _CONNECTION_DROPPED_ERROR_SUBSTRINGS = (# no-SSL-support source fails with a different message ("server does not support SSL") and, on# require_ssl sources, is converted to SSLRequiredError before reaching this check."ssl connection has been closed unexpectedly",+ # The lower-level form of the same TLS drop: a socket-level failure during the SSL handshake or+ # read (EOF, connection reset) surfaces as "... SSL SYSCALL error: EOF detected" (and similar).+ # Same transient class as the bare SSL drop above — a pooler/firewall idle cull, failover, or+ # network blip mid-handshake — and recovers on reconnect. Never the unsupported-SSL signal,+ # which is the distinct "server does not support SSL" message.+ "ssl syscall error","no connection to the server","terminating connection due to",# psycopg's own message when libpq finds the socket already gone (PGconn.socket@@ -540,7 +546,12 @@ def _connect_to_postgres(**kwargs,)except psycopg.OperationalError as e:- if require_ssl and "SSL" in str(e) and not _is_invalid_ssl_negotiation_response(e):+ if (+ require_ssl+ and "SSL" in str(e)+ and not _is_invalid_ssl_negotiation_response(e)+ and not _is_connection_dropped_error(e)+ ):raise SSLRequiredError("SSL/TLS connection is required but your database does not support it. ""Please enable SSL/TLS on your PostgreSQL server or contact your database administrator."@@ -2539,7 +2550,12 @@ def postgres_source(options=FORCE_UTF8_CLIENT_ENCODING,)except psycopg.OperationalError as e:- if require_ssl and "SSL" in str(e) and not _is_invalid_ssl_negotiation_response(e):+ if (+ require_ssl+ and "SSL" in str(e)+ and not _is_invalid_ssl_negotiation_response(e)+ and not _is_connection_dropped_error(e)+ ):raise SSLRequiredError("SSL/TLS connection is required but your database does not support it. ""Please enable SSL/TLS on your PostgreSQL server or contact your database administrator."@@ -2858,7 +2874,12 @@ def postgres_source(options=FORCE_UTF8_CLIENT_ENCODING,)except psycopg.OperationalError as e:- if require_ssl and "SSL" in str(e) and not _is_invalid_ssl_negotiation_response(e):+ if (+ require_ssl+ and "SSL" in str(e)+ and not _is_invalid_ssl_negotiation_response(e)+ and not _is_connection_dropped_error(e)+ ):raise SSLRequiredError("SSL/TLS connection is required but your database does not support it. ""Please enable SSL/TLS on your PostgreSQL server or contact your database administrator."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.pyindex 0879ba9c63a6..2e3cd1dd34ac 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@@ -990,6 +990,13 @@ class TestIsConnectionDroppedError:'connection failed: connection to server at "10.0.0.1", port 5432 failed: '"SSL connection has been closed unexpectedly"),+ # libpq's lower-level form of the same TLS drop — a socket-level EOF/reset during the+ # SSL handshake or read. Transient (pooler/firewall idle cull, failover, network blip),+ # not the permanent no-SSL-support case, so the in-process reconnect must catch it.+ psycopg.OperationalError(+ 'connection failed: connection to server at "10.0.0.1", port 5432 failed: '+ "SSL SYSCALL error: EOF detected"+ ),psycopg.OperationalError("terminating connection due to administrator command"),# psycopg's message when libpq finds the socket already gone (raised from# PGconn.socket inside the commit at the end of get_connection). A transient@@ -1449,6 +1456,33 @@ class TestInvalidSSLNegotiationResponse:host="db", port=5432, database="db", user="postgres", password="x", require_ssl=True)+ @pytest.mark.parametrize(+ "message",+ [+ "SSL connection has been closed unexpectedly",+ "SSL SYSCALL error: EOF detected",+ ],+ )+ def test_transient_ssl_drop_is_not_wrapped_as_ssl_required(self, message):+ # require_ssl=True, but the message is a transient TLS drop (pooler/firewall idle cull,+ # failover, network blip) — it must surface raw so the connect-retry / Temporal can treat+ # it as retryable, not as the non-retryable SSLRequiredError, which would permanently stop+ # the sync with a misleading "enable SSL on your server" message.+ connect_mock = mock.MagicMock(+ side_effect=psycopg.OperationalError(+ f'connection failed: connection to server at "10.0.0.1", port 5432 failed: {message}'+ )+ )+ with patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.postgres.postgres.psycopg.connect",+ connect_mock,+ ):+ with pytest.raises(psycopg.OperationalError) as exc_info:+ _connect_to_postgres(+ host="db", port=5432, database="db", user="postgres", password="x", require_ssl=True+ )+ assert not isinstance(exc_info.value, SSLRequiredError)+class TestStatementTimeoutAsNonRetryable:@pytest.mark.parametrize(