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(redshift): retry a transient connection drop opening the streaming connection (#105515)

Tom Owersf79b315f836f
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 1c5d9fef8130..4b183a160455 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
@@ -14,7 +14,7 @@ import re
import time
import collections
from collections.abc import Callable, Iterator
-from contextlib import contextmanager
+from contextlib import ExitStack, contextmanager
from datetime import UTC, date, datetime, timezone
from typing import Any, Literal, LiteralString, Optional, TypeVar, cast
@@ -2033,7 +2033,14 @@ class RedshiftImplementation(SQLSourceImplementation[RedshiftSourceConfig, psyco
return _resolve_projection(fresh_table, primary_keys)
def get_rows() -> Iterator[Any]:
- with self.connect(config, team_id=inputs.team_id) as streaming_connection:
+ with ExitStack() as stack:
+ # A fresh connection can drop before the first batch is read too, just like during
+ # setup above — retrying only the open is safe here since nothing has been yielded
+ # yet. `ExitStack.enter_context` only registers cleanup after `__enter__` succeeds,
+ # so a failed attempt leaves nothing to tear down and the next attempt opens clean.
+ streaming_connection = _retry_on_transient_connection_drop(
+ lambda: stack.enter_context(self.connect(config, team_id=inputs.team_id)), logger
+ )
streaming_connection.adapters.register_loader("json", JsonAsStringLoader)
projection = _refreshed_projection(streaming_connection)
table = projection.table
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 e3950d2fbe9b..4c501c4f90b1 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
@@ -1755,6 +1755,37 @@ class TestBuildPipeline:
with pytest.raises(psycopg.OperationalError):
impl.build_pipeline(_make_config(), _make_inputs())
+ def test_retries_once_on_transient_connection_drop_opening_the_streaming_connection(
+ self, build_pipeline_mocks, mocker
+ ):
+ # Regression: unlike the metadata connect above, opening the streaming connection had no
+ # in-process retry. Nothing has been read yet at that point, so a drop there is exactly as
+ # safe to retry as a setup-phase drop — but without the retry it fell straight through to a
+ # full Temporal activity retry that restarts the whole sync.
+ mocker.patch("products.warehouse_sources.backend.temporal.data_imports.sources.redshift.redshift.time.sleep")
+ mock_connect, streaming_cursor = build_pipeline_mocks
+ real_side_effect = mock_connect.side_effect
+ attempts = {"n": 0}
+
+ def flaky_streaming_connect(*args, **kwargs):
+ attempts["n"] += 1
+ # Call 1 is the metadata connect; call 2 is the first streaming connect attempt.
+ if attempts["n"] == 2:
+ raise psycopg.OperationalError("the connection is lost")
+ return real_side_effect(*args, **kwargs)
+
+ mock_connect.side_effect = flaky_streaming_connect
+
+ impl = RedshiftImplementation()
+ response = impl.build_pipeline(_make_config(), _make_inputs())
+ list(response.items()) # type: ignore[arg-type]
+
+ assert attempts["n"] == 3
+ # The metadata connect's own `SET statement_timeout` also calls `execute` on this shared
+ # cursor mock, so asserting `execute.called` would pass even if the retried streaming
+ # connection never ran its query. `stream` is only called once the retry succeeds.
+ streaming_cursor.stream.assert_called_once()
+
def test_returns_source_response(self, build_pipeline_mocks):
mock_connect, _ = build_pipeline_mocks
impl = RedshiftImplementation()