: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(mysql): retry broken-pipe errors during connect as transient (#73392)
Tom Owers8786647bb612
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.pyindex 5ddc74179a78..e962d6ff5b9d 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@@ -462,6 +462,26 @@ def _is_transient_connect_reset(e: BaseException) -> bool:return _CONNECTION_RESET_TOKEN in " ".join(str(arg) for arg in e.args)+# EPIPE at connect time: pymysql wraps a write to an already-closed socket — e.g. sending the+# auth packet or the TLS handshake — as the 2003 "Can't connect to MySQL server on '<host>'+# ([Errno 32] Broken pipe)" failure. It is the write-side sibling of the `Connection reset by+# peer` case above (which fires on the read side): the peer, often a TCP proxy or load balancer+# in front of the DB, accepted the connection then closed it while pymysql was still writing —+# an overloaded server, a proxy idle cull, or a backend cycling. A fresh attempt usually reaches+# a healthy backend. Match the stable strerror phrase, not the volatile host.+_BROKEN_PIPE_TOKEN = "Broken pipe"+++def _is_transient_connect_broken_pipe(e: BaseException) -> bool:+ """Return True if writing to the peer failed with a broken pipe during connect — a transient blip."""+ 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 _BROKEN_PIPE_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"@@ -523,6 +543,7 @@ def _connect_with_transient_retry(kwargs: dict[str, Any]) -> pymysql.Connection:or _is_transient_connect_timeout(e)or _is_transient_connect_dns_failure(e)or _is_transient_connect_reset(e)+ or _is_transient_connect_broken_pipe(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.pyindex 59bef0def104..f381e502327f 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@@ -25,6 +25,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.mysql.mysqMySQLImplementation,_build_query,_is_bad_plan_error,+ _is_transient_connect_broken_pipe,_is_transient_connect_dns_failure,_is_transient_connect_drop,_is_transient_connect_gone_away,@@ -1092,6 +1093,43 @@ class TestIsTransientConnectReset:assert not _is_transient_connect_reset(pymysql.err.OperationalError())+class TestIsTransientConnectBrokenPipe:+ def test_matches_broken_pipe_on_connect(self):+ # EPIPE at connect time — a write to an already-closed socket (e.g. sending the auth+ # packet through a proxy that cycled its backend). A transient blip that must be retried+ # in-process rather than surfacing as the non-retryable "Can't connect" config error.+ assert _is_transient_connect_broken_pipe(+ pymysql.err.OperationalError(+ 2003, "Can't connect to MySQL server on 'db.example.com' ([Errno 32] Broken pipe)"+ )+ )++ @pytest.mark.parametrize(+ "message",+ [+ # Refused connection and failed DNS lookup are also 2003 but deterministic host/port+ # misconfig — they must stay non-retryable, not be absorbed here.+ "Can't connect to MySQL server on 'db.example.com' ([Errno 111] Connection refused)",+ "Can't connect to MySQL server on 'nope.example.com' ([Errno -2] Name or service not known)",+ ],+ )+ def test_does_not_match_permanent_connect_errors(self, message):+ assert not _is_transient_connect_broken_pipe(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_broken_pipe(pymysql.err.OperationalError(code, message))++ def test_does_not_match_error_without_args(self):+ assert not _is_transient_connect_broken_pipe(pymysql.err.OperationalError())++class TestIsTransientPacketSequenceError:@pytest.mark.parametrize("message",