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(warehouse): retry Redshift sync setup on connect-time drops (#89819)

posthog[bot]be3ee9d56349
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/redshift/redshift.py b/products/warehouse_sources/backend/temporal/data_imports/sources/redshift/redshift.py
index e6d3adaf8374..82c14c82dc70 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/redshift/redshift.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/redshift/redshift.py
@@ -402,14 +402,29 @@ _T = TypeVar("_T")
_MAX_SETUP_CONNECTION_DROP_ATTEMPTS = 3
+# Substrings psycopg uses for a transient socket drop around sync setup. "the connection is lost"
+# is the message libpq gives when an already-open connection dies. "server closed the connection
+# unexpectedly" is the message when the socket dies during the connect handshake ("connection
+# failed: ... server closed the connection unexpectedly"). Both are the same transient class — a
+# network blip or a cluster pause/resize — and recover by reconnecting. Keep this narrow so a
+# permanent failure such as "password authentication failed" is never retried in-process.
+_TRANSIENT_CONNECTION_DROP_SUBSTRINGS = (
+ "the connection is lost",
+ "server closed the connection unexpectedly",
+)
+
+
def _is_transient_connection_drop_error(error: BaseException) -> bool:
"""True if a freshly opened connection died before `build_pipeline`'s setup phase finished.
- psycopg raises this exact OperationalError message when libpq finds the socket already gone
- (a network blip or a cluster pause/resize) — the keepalives configured on connect only detect
- a dead peer during a query, not this class of drop between connecting and the first query.
+ The keepalives configured on connect only detect a dead peer during a query, not a drop
+ between connecting and the first query, so this matches the messages psycopg raises for both
+ a mid-handshake drop and an already-open connection that dies.
"""
- return isinstance(error, psycopg.OperationalError) and "the connection is lost" in str(error)
+ if not isinstance(error, psycopg.OperationalError):
+ return False
+ message = str(error)
+ return any(substring in message for substring in _TRANSIENT_CONNECTION_DROP_SUBSTRINGS)
def _retry_on_transient_connection_drop(
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/redshift/tests/test_redshift.py b/products/warehouse_sources/backend/temporal/data_imports/sources/redshift/tests/test_redshift.py
index b9f68e89bf42..81b9030977cb 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/redshift/tests/test_redshift.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/redshift/tests/test_redshift.py
@@ -1380,6 +1380,16 @@ class TestIsTransientConnectionDropError:
def test_matches_connection_is_lost(self):
assert _is_transient_connection_drop_error(psycopg.OperationalError("the connection is lost")) is True
+ def test_matches_connect_time_server_closed(self):
+ # A drop during the connect handshake surfaces as a different message than an already-open
+ # connection dying, so the guard must match it too or it re-raises on the first attempt.
+ assert (
+ _is_transient_connection_drop_error(
+ psycopg.OperationalError("connection failed: server closed the connection unexpectedly")
+ )
+ is True
+ )
+
def test_does_not_match_unrelated_operational_error(self):
# A permanent, non-actionable failure that also raises OperationalError must not be
# swept up by the narrow "the connection is lost" match and retried in-process.