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(mysql): retry transient DNS resolution failures on connect (#70421)

Tom Owerscfe4d223b5f7
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/mysql.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/mysql.py
index 1960b826291d..69376976dfbf 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/mysql.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/mysql.py
@@ -381,6 +381,27 @@ def _is_transient_connect_timeout(e: BaseException) -> bool:
return "timed out" in " ".join(str(arg) for arg in e.args)
+# glibc's getaddrinfo returns EAI_AGAIN — "Temporary failure in name resolution" — when the DNS
+# resolver is momentarily unavailable (the nameserver couldn't be reached, returned SERVFAIL, or a
+# resolver-side blip prevented the lookup). pymysql wraps it as the 2003 connect failure, so without
+# this it falls through to the non-retryable "Can't connect to MySQL server on" classifier and the
+# sync gives up on a recoverable blip. EAI_AGAIN is a "try again later" condition — distinct from
+# EAI_NONAME ("Name or service not known"), where the host genuinely doesn't resolve and staying
+# non-retryable is correct. Match the stable, locale-independent gai_strerror text rather than the
+# platform-specific errno number.
+_DNS_TEMPORARY_FAILURE_TOKEN = "Temporary failure in name resolution"
+
+
+def _is_transient_connect_dns_failure(e: BaseException) -> bool:
+ """Return True if connect failed on a transient DNS-resolver blip — recoverable on retry."""
+ if not isinstance(e, pymysql.err.OperationalError):
+ return False
+ code = e.args[0] if e.args else None
+ if code != _CANT_CONNECT_CODE:
+ return False
+ return _DNS_TEMPORARY_FAILURE_TOKEN in " ".join(str(arg) for arg in e.args)
+
+
# pymysql raises this `InternalError` from `_read_packet` when an incoming packet's
# sequence number doesn't match the expected one (it `_force_close()`s the socket first).
_PACKET_SEQUENCE_ERROR_PHRASE = "Packet sequence number wrong"
@@ -439,6 +460,7 @@ def _connect_with_transient_retry(kwargs: dict[str, Any]) -> pymysql.Connection:
if attempt >= _MAX_CONNECT_ATTEMPTS or not (
_is_transient_connect_drop(e)
or _is_transient_connect_timeout(e)
+ or _is_transient_connect_dns_failure(e)
or _is_transient_packet_sequence_error(e)
or _is_transient_vitess_dial_timeout(e)
):
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/tests/test_mysql.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/tests/test_mysql.py
index 98ba6b319f85..7a8626f6954d 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/tests/test_mysql.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mysql/tests/test_mysql.py
@@ -24,6 +24,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.mysql.mysq
MySQLImplementation,
_build_query,
_is_bad_plan_error,
+ _is_transient_connect_dns_failure,
_is_transient_connect_drop,
_is_transient_connect_timeout,
_is_transient_packet_sequence_error,
@@ -951,6 +952,43 @@ class TestIsTransientConnectTimeout:
assert not _is_transient_connect_timeout(pymysql.err.OperationalError())
+class TestIsTransientConnectDnsFailure:
+ def test_matches_temporary_name_resolution_failure(self):
+ # glibc EAI_AGAIN — a transient resolver blip that a fresh attempt recovers from, so it must
+ # be retried in-process rather than surfacing as the non-retryable "Can't connect" config error.
+ assert _is_transient_connect_dns_failure(
+ pymysql.err.OperationalError(
+ 2003,
+ "Can't connect to MySQL server on 'db.example.com' ([Errno -3] Temporary failure in name resolution)",
+ )
+ )
+
+ @pytest.mark.parametrize(
+ "message",
+ [
+ # EAI_NONAME — the host genuinely doesn't resolve; a deterministic config error that must
+ # stay non-retryable rather than being absorbed as a transient blip here.
+ "Can't connect to MySQL server on 'nope.example.com' ([Errno -2] Name or service not known)",
+ "Can't connect to MySQL server on 'db.example.com' ([Errno 111] Connection refused)",
+ ],
+ )
+ def test_does_not_match_permanent_connect_errors(self, message):
+ assert not _is_transient_connect_dns_failure(pymysql.err.OperationalError(2003, message))
+
+ @pytest.mark.parametrize(
+ "code,message",
+ [
+ (2013, "Lost connection to MySQL server during query"),
+ (1045, "Access denied for user"),
+ ],
+ )
+ def test_does_not_match_other_error_codes(self, code, message):
+ assert not _is_transient_connect_dns_failure(pymysql.err.OperationalError(code, message))
+
+ def test_does_not_match_error_without_args(self):
+ assert not _is_transient_connect_dns_failure(pymysql.err.OperationalError())
+
+
class TestIsTransientPacketSequenceError:
@pytest.mark.parametrize(
"message",
@@ -1077,6 +1115,28 @@ class TestConnectTransientRetry:
assert mock_connect.call_count == 2
sleep.assert_called_once_with(2)
+ def test_retries_dns_temporary_failure_then_succeeds(self, mocker):
+ sleep = mocker.patch("products.warehouse_sources.backend.temporal.data_imports.sources.mysql.mysql.time.sleep")
+ conn = MagicMock()
+ conn.__enter__.return_value = conn
+ mock_connect = mocker.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.mysql.mysql.pymysql.connect",
+ side_effect=[
+ pymysql.err.OperationalError(
+ 2003,
+ "Can't connect to MySQL server on 'db.example.com' "
+ "([Errno -3] Temporary failure in name resolution)",
+ ),
+ conn,
+ ],
+ )
+
+ with MySQLImplementation().connect(_make_config()) as yielded:
+ assert yielded is conn
+
+ assert mock_connect.call_count == 2
+ sleep.assert_called_once_with(2)
+
def test_retries_vitess_dial_timeout_then_succeeds(self, mocker):
sleep = mocker.patch("products.warehouse_sources.backend.temporal.data_imports.sources.mysql.mysql.time.sleep")
conn = MagicMock()