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): treat TLS ALPN rejection as non-retryable (#64142)

Tom Owers68b54468f0f3
diff --git a/posthog/temporal/data_imports/sources/postgres/source.py b/posthog/temporal/data_imports/sources/postgres/source.py
index d3bd5c18442f..93a474043680 100644
--- a/posthog/temporal/data_imports/sources/postgres/source.py
+++ b/posthog/temporal/data_imports/sources/postgres/source.py
@@ -238,6 +238,23 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDa
"No route to host": None,
"password authentication failed connection": None,
"connection timeout expired": None,
+ # TLS ALPN alert (RFC 7301 "no_application_protocol", alert 120) sent by the server
+ # during the TLS handshake. libpq (Postgres 17+) offers the "postgresql" ALPN protocol;
+ # an endpoint that negotiates ALPN but doesn't accept it rejects the handshake outright.
+ # In practice this means the configured host/port isn't a PostgreSQL server speaking TLS
+ # — it's an HTTP/proxy/edge endpoint, or simply the wrong port — so retrying re-runs into
+ # the same rejection. The raw psycopg message ("... SSL error: tlsv1 alert no application
+ # protocol") only matches when require_ssl=False leaves it unwrapped; with require_ssl=True
+ # it's surfaced as SSLRequiredError below instead. Match the stable alert text and exclude
+ # the volatile host/port so the condition is caught on both paths. Placed before the
+ # generic SSL entries so its accurate message wins if both happen to match.
+ "no application protocol": (
+ "PostHog couldn't complete a TLS handshake with the host and port you configured — "
+ 'the server rejected the connection during TLS negotiation ("no application '
+ "protocol\"). This usually means the host and port don't point at a PostgreSQL server "
+ "speaking TLS (for example an HTTP, proxy, or edge endpoint, or the wrong port). "
+ "Check your host and port, then re-enable the sync."
+ ),
"SSLRequiredError": None,
"SSL/TLS connection is required": None,
"Could not establish session to SSH gateway": None,
diff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
index c28ae28ccbd6..b75a584d8351 100644
--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
@@ -271,6 +271,32 @@ class TestPostgresSourceNonRetryableErrors:
is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())
assert is_non_retryable, f"Permanent error should be non-retryable: {error_msg}"
+ @pytest.mark.parametrize(
+ "error_msg",
+ [
+ # Raw psycopg message (what the activity-level check sees via str(e)) when require_ssl=False
+ # leaves the OperationalError unwrapped. The host/port are volatile; the alert text is stable.
+ 'connection failed: connection to server at "37.16.27.102", port 6432 failed: SSL error: tlsv1 alert no application protocol',
+ 'connection failed: connection to server at "10.0.0.1", port 5432 failed: SSL error: tlsv1 alert no application protocol',
+ # Temporal-wrapped message (what the workflow-level check sees) — carries the class name.
+ 'OperationalError: connection failed: connection to server at "37.16.27.102", port 6432 failed: SSL error: tlsv1 alert no application protocol',
+ ],
+ )
+ def test_tls_no_application_protocol_errors_are_non_retryable(self, source, error_msg):
+ non_retryable = source.get_non_retryable_errors()
+ is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())
+ assert is_non_retryable, f"TLS ALPN rejection error should be non-retryable: {error_msg}"
+
+ def test_tls_no_application_protocol_returns_friendly_message(self, source):
+ non_retryable = source.get_non_retryable_errors()
+ error_msg = (
+ 'connection failed: connection to server at "37.16.27.102", port 6432 failed: '
+ "SSL error: tlsv1 alert no application protocol"
+ )
+ friendly = [reason for pattern, reason in non_retryable.items() if pattern in error_msg and reason]
+ assert friendly, "TLS ALPN rejection error should surface an actionable message"
+ assert "host and port" in friendly[0]
+
def test_supavisor_enotfound_tenant_user_uses_new_key(self, source):
# The older tenant/user patterns don't cover the newer "(ENOTFOUND) tenant/user" wording,
# so confirm it's specifically the new key that recognises this message.