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 SSLZeroReturnError peer-close on connect (#70542)

Tom Owers62fede4b9264
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 69376976dfbf..c1fca512b08e 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
@@ -321,6 +321,13 @@ _CANT_CONNECT_TO_SERVER_CODE = 2003
# Postgres source, which retries its own "SSL connection has been closed unexpectedly" on connect.
_SSL_UNEXPECTED_EOF_TOKEN = "[SSL: UNEXPECTED_EOF_WHILE_READING]"
+# Newer OpenSSL/Python raise `ssl.SSLZeroReturnError` — "TLS/SSL connection has been closed (EOF)" —
+# for the same peer-closed-the-TLS-connection condition the token above covers, just a different
+# rendering (a clean SSL_ERROR_ZERO_RETURN close rather than an abrupt read EOF). pymysql wraps it as
+# the same 2003 connect failure, so it's equally transient and must be retried too. Match the stable
+# phrase, not the volatile `_ssl.c:<line>` suffix that shifts across Python builds.
+_SSL_CONNECTION_CLOSED_EOF_TOKEN = "TLS/SSL connection has been closed (EOF)"
+
# paramiko raises a bare, message-less EOFError from `start_client` when the SSH gateway accepts
# the TCP connection but closes it during the SSH handshake — a non-SSH service on the port, a
# bastion refusing PostHog's IPs, or a proxy that resets the stream. sshtunnel doesn't wrap it
@@ -344,10 +351,12 @@ def _is_transient_connect_drop(e: BaseException) -> bool:
A fresh attempt recovers. The one 2013 that is *not* transient is the SSL-version
mismatch (a deterministic config error, already non-retryable): it arrives with an
`[SSL: ...` suffix, so exclude that and let it surface.
- - `2003` (can't connect) carrying an `[SSL: UNEXPECTED_EOF_WHILE_READING]` cause:
- the peer aborted the TLS handshake with an unexpected EOF — the SSL-flavoured
- sibling of the 2013 drop, equally transient. The generic 2003 (wrong host/port,
- firewall) stays non-retryable, so match only the unexpected-EOF token.
+ - `2003` (can't connect) carrying an SSL peer-close cause — either
+ `[SSL: UNEXPECTED_EOF_WHILE_READING]` (an abrupt read EOF) or "TLS/SSL connection
+ has been closed (EOF)" (`SSLZeroReturnError`, a clean close): the peer dropped the
+ TLS connection mid-handshake — the SSL-flavoured sibling of the 2013 drop, equally
+ transient. The generic 2003 (wrong host/port, firewall) stays non-retryable, so
+ match only the SSL peer-close tokens.
"""
if not isinstance(e, pymysql.err.OperationalError):
return False
@@ -356,7 +365,7 @@ def _is_transient_connect_drop(e: BaseException) -> bool:
if code == _LOST_CONNECTION_DURING_QUERY_CODE:
return "[SSL:" not in args_text
if code == _CANT_CONNECT_TO_SERVER_CODE:
- return _SSL_UNEXPECTED_EOF_TOKEN in args_text
+ return _SSL_UNEXPECTED_EOF_TOKEN in args_text or _SSL_CONNECTION_CLOSED_EOF_TOKEN in args_text
return False
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 7a8626f6954d..6728d9824a9f 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
@@ -887,17 +887,21 @@ class TestIsTransientConnectDrop:
)
)
- def test_matches_ssl_unexpected_eof_on_connect(self):
- # The peer aborted the TLS handshake with an unexpected EOF, wrapped by pymysql as the
- # 2003 connect failure. A transient drop (overloaded server, proxy idle cull, failover) —
- # the in-process retry must catch it instead of letting the first blip surface as noise.
- assert _is_transient_connect_drop(
- pymysql.err.OperationalError(
- 2003,
- "Can't connect to MySQL server on 'db.example.com' "
- "([SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1032))",
- )
- )
+ @pytest.mark.parametrize(
+ "message",
+ [
+ # The peer aborted the TLS handshake with an unexpected read EOF.
+ "Can't connect to MySQL server on 'db.example.com' "
+ "([SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1032))",
+ # The `SSLZeroReturnError` rendering of the same peer-closed-the-TLS-connection drop.
+ "Can't connect to MySQL server on 'db.example.com' (TLS/SSL connection has been closed (EOF) (_ssl.c:1032))",
+ ],
+ )
+ def test_matches_ssl_peer_close_on_connect(self, message):
+ # pymysql wraps an SSL peer-close mid-handshake as the 2003 connect failure. A transient
+ # drop (overloaded server, proxy idle cull, failover) — the in-process retry must catch it
+ # instead of letting the first blip surface as the non-retryable "Can't connect" config error.
+ assert _is_transient_connect_drop(pymysql.err.OperationalError(2003, message))
@pytest.mark.parametrize(
"code,message",