: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(mongodb): treat server-selection timeouts as non-retryable (#63809)
Tom Owers6eef78ee7364
diff --git a/posthog/temporal/data_imports/sources/mongodb/source.py b/posthog/temporal/data_imports/sources/mongodb/source.pyindex a1b6ae564ac2..f1190394b8f3 100644--- a/posthog/temporal/data_imports/sources/mongodb/source.py+++ b/posthog/temporal/data_imports/sources/mongodb/source.py@@ -52,13 +52,19 @@ class MongoDBSource(SimpleSource[MongoDBSourceConfig], ValidateDatabaseHostMixin"AuthenticationFailed": auth_failed_msg,"Authentication failed": auth_failed_msg,"SSL handshake failed": None,- # pymongo raises ServerSelectionTimeoutError ("No servers found yet" / "No replica set- # members found yet") when it can't reach any cluster node for the whole selection- # timeout. On a managed cluster this is a persistent connectivity problem — the worker- # IP isn't allowlisted, or the cluster is paused/decommissioned — not a momentary blip- # (a mid-sync outage surfaces differently), so retrying the job won't recover it.- "No servers found yet": _MONGO_UNREACHABLE_MESSAGE,- "No replica set members found yet": _MONGO_UNREACHABLE_MESSAGE,+ # pymongo raises ServerSelectionTimeoutError when it can't select a usable cluster node+ # for the whole selection timeout. The reason varies — "No servers found yet" / "No+ # replica set members found yet" when nothing was ever discovered, or a per-server+ # "<host>: connection closed ... error=AutoReconnect(...)" when a host resolves but every+ # connection attempt is dropped for the entire window. All of these carry the+ # "Topology Description:" suffix that only ServerSelectionTimeoutError emits, so we key+ # off that single marker. On a managed cluster this is a persistent connectivity problem+ # — the worker IP isn't allowlisted, the cluster is paused/decommissioned, or the+ # connection string points at an endpoint the driver can't speak to — not a momentary+ # blip, so retrying the job won't recover it. A transient mid-sync drop surfaces+ # differently (a bare AutoReconnect / NetworkTimeout with no topology description) and+ # stays retryable.+ "Topology Description:": _MONGO_UNREACHABLE_MESSAGE,}def get_schemas(diff --git a/posthog/temporal/data_imports/sources/mongodb/test_mongo.py b/posthog/temporal/data_imports/sources/mongodb/test_mongo.pyindex b8d0abd90027..0545071ab09b 100644--- a/posthog/temporal/data_imports/sources/mongodb/test_mongo.py+++ b/posthog/temporal/data_imports/sources/mongodb/test_mongo.py@@ -337,13 +337,27 @@ class TestMongoDBNonRetryableErrors(SimpleTestCase):),("dns_failure", "The DNS query name does not exist: example.mongodb.net."),("ssl_failure", "SSL handshake failed: certificate verify failed"),- # Cluster unreachable for the whole selection timeout — persistent connectivity issue.+ # ServerSelectionTimeoutError variants — cluster unreachable for the whole selection+ # timeout. All carry the "Topology Description:" suffix regardless of the per-reason text.("no_servers", "No servers found yet, Timeout: 5.0s, Topology Description: ..."),("no_replica_set_members","No replica set members found yet, Timeout: 10.0s, Topology Description: ""<TopologyDescription topology_type: ReplicaSetNoPrimary>",),+ # Host resolves but every connection attempt is closed for the whole window — the driver+ # never identifies the server (topology_type: Unknown) and wraps the per-server+ # AutoReconnect. Persistent connectivity/config problem, not a momentary blip.+ (+ "connection_closed_selection_timeout",+ "cluster0.example.mongodb.net:27017: connection closed (configured timeouts: "+ "socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms), Timeout: 10.0s, "+ "Topology Description: <TopologyDescription id: abc, topology_type: Unknown, "+ "servers: [<ServerDescription ('cluster0.example.mongodb.net', 27017) "+ "server_type: Unknown, rtt: None, error=AutoReconnect('cluster0.example.mongodb.net:"+ "27017: connection closed (configured timeouts: socketTimeoutMS: 20000.0ms, "+ "connectTimeoutMS: 20000.0ms)')>]>",+ ),])def test_known_errors_are_non_retryable(self, _name, error_msg):@@ -355,6 +369,14 @@ class TestMongoDBNonRetryableErrors(SimpleTestCase):[("connection_reset", "connection closed"),("network_timeout", "NetworkTimeout: timed out reading from socket"),+ # A mid-sync AutoReconnect carries the "(configured timeouts: ...)" suffix but no+ # topology description — it's a momentary drop the driver can recover from, so it must+ # not be caught by the "Topology Description:" server-selection matcher.+ (+ "mid_sync_auto_reconnect",+ "cluster0.example.mongodb.net:27017: connection closed (configured timeouts: "+ "socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms)",+ ),])def test_transient_errors_are_retryable(self, _name, error_msg):@@ -366,8 +388,7 @@ class TestMongoDBNonRetryableErrors(SimpleTestCase):[("code_name", "AuthenticationFailed", "password"),("message", "Authentication failed", "password"),- ("unreachable_no_servers", "No servers found yet", "allowlist"),- ("unreachable_no_replica_set", "No replica set members found yet", "allowlist"),+ ("unreachable_topology", "Topology Description:", "allowlist"),])def test_pattern_has_friendly_message(self, _name, pattern, expected_substring):