: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(redshift): bound stalled discovery connections with TCP keepalives (#64162)
Tom Owers7c0c5ab07393
diff --git a/posthog/temporal/data_imports/sources/redshift/redshift.py b/posthog/temporal/data_imports/sources/redshift/redshift.pyindex 3c9d9a1c6810..5a3b2ab83412 100644--- a/posthog/temporal/data_imports/sources/redshift/redshift.py+++ b/posthog/temporal/data_imports/sources/redshift/redshift.py@@ -67,9 +67,25 @@ __all__ = [# cert paths are intentionally pointed at non-existent files so psycopg# uses the system default verification without picking up an unintended# client cert.+#+# TCP keepalives bound a *post-connect* socket stall. `connect_timeout` only+# covers establishing the connection — once connected, a query blocks in+# psycopg's `wait_c` for as long as the socket stays open with no response.+# If the Redshift peer goes away mid-query (cluster pause/resize, network+# drop), that wait would otherwise hang until the Temporal activity hits its+# `start_to_close_timeout` and cancels the worker thread, surfacing a+# misleading `CancelledError` and burning the whole activity budget. With+# keepalives a dead peer is detected in ~30-60s and raised as a fast,+# retryable `OperationalError` instead. These only fire when the peer stops+# responding, so they never interrupt a healthy long-running streaming sync._REDSHIFT_CONNECT_OPTS: dict[str, Any] = {"sslmode": "require","connect_timeout": 15,+ "keepalives": 1,+ "keepalives_idle": 30,+ "keepalives_interval": 10,+ "keepalives_count": 3,+ "tcp_user_timeout": 60000, # 60s: force-close if sent data stays unacked"sslrootcert": "/tmp/no.txt","sslcert": "/tmp/no.txt","sslkey": "/tmp/no.txt",diff --git a/posthog/temporal/data_imports/sources/redshift/tests/test_redshift.py b/posthog/temporal/data_imports/sources/redshift/tests/test_redshift.pyindex 9a0d633d713f..eafdf9318eb6 100644--- a/posthog/temporal/data_imports/sources/redshift/tests/test_redshift.py+++ b/posthog/temporal/data_imports/sources/redshift/tests/test_redshift.py@@ -857,3 +857,37 @@ class TestBuildPipeline:# Migrated row keeps its original subdir rather than moving to `analytics_users`.assert response.name == "users"+++# ---------------------------------------------------------------------------+# Connection lifecycle+# ---------------------------------------------------------------------------+++class TestConnect:+ def test_connect_forwards_tcp_keepalive_opts(self, mocker):+ # Regression: a discovery query (`get_columns`) hung in psycopg's `wait_c` on a dead+ # connection until the Temporal activity's `start_to_close_timeout` cancelled the worker+ # thread, surfacing a misleading `CancelledError`. `connect_timeout` only bounds+ # establishing the connection, so the connection must enable TCP keepalives to detect a+ # dead peer mid-query and fail fast with a retryable error instead.+ mocker.patch(+ "posthog.temporal.data_imports.sources.redshift.redshift.open_ssh_tunnel",+ ).return_value.__enter__.return_value = ("localhost", 5439)+ mock_conn = MagicMock()+ mock_conn.__enter__.return_value = mock_conn+ mock_connect = mocker.patch(+ "posthog.temporal.data_imports.sources.redshift.redshift.psycopg.connect",+ return_value=mock_conn,+ )++ impl = RedshiftImplementation()+ with impl.connect(_make_config()):+ pass++ kwargs = mock_connect.call_args.kwargs+ assert kwargs["keepalives"] == 1+ assert kwargs["keepalives_idle"] == 30+ assert kwargs["keepalives_interval"] == 10+ assert kwargs["keepalives_count"] == 3+ assert kwargs["tcp_user_timeout"] == 60000