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): replace invalid bytes instead of failing the row decode (#116723)

Tom Owersa279de98dfc7
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 10d7cde61bfb..79b8baf1439d 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
@@ -35,6 +35,7 @@ from pymysql.charset import charset_by_id
from pymysql.connections import MySQLResult
from pymysql.constants import CLIENT, CR, FIELD_TYPE
from pymysql.cursors import Cursor, SSCursor
+from pymysql.protocol import MysqlPacket
from structlog.types import FilteringBoundLogger
# Module-level error-capture seam. This module's best-effort probes (get_rows_to_sync,
@@ -787,6 +788,9 @@ class _FieldCharsetResult(MySQLResult):
pymysql decodes every column outside its own text types as ASCII. Some MySQL-compatible
servers send ENUM and SET values under their own type codes, and newer servers add types
pymysql predates, so one non-ASCII value in such a column fails the whole read.
+
+ The server does not check that stored bytes are valid in the charset it reports, so rows are
+ decoded with invalid bytes replaced by U+FFFD rather than failing every sync on one bad value.
"""
def _get_descriptions(self) -> None:
@@ -797,6 +801,21 @@ class _FieldCharsetResult(MySQLResult):
if encoding == "ascii" and field.type_code not in _ASCII_FIELD_TYPES:
self.converters[index] = (_field_encoding(field.charsetnr, connection_encoding), converter) # type: ignore[attr-defined]
+ def _read_row_from_packet(self, packet: MysqlPacket) -> tuple[Any, ...]:
+ row: list[Any] = []
+ for encoding, converter in self.converters: # type: ignore[attr-defined]
+ try:
+ data = packet.read_length_coded_string()
+ except IndexError:
+ break
+ if data is not None:
+ if encoding is not None:
+ data = data.decode(encoding, errors="replace")
+ if converter is not None:
+ data = converter(data)
+ row.append(data)
+ return tuple(row)
+
def _set_tcp_liveness(sock: socket.socket) -> None:
try:
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 0281f472afe4..b5595e5bcb78 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
@@ -217,6 +217,8 @@ class TestFieldCharsetResult:
[
(FIELD_TYPE.ENUM, 8, "ao\u00fbt".encode("latin1"), "ao\u00fbt"),
(FIELD_TYPE.SET, 255, "co\u00fbt".encode(), "co\u00fbt"),
+ (FIELD_TYPE.ENUM, 255, "ao\u00fbt".encode("latin1"), "ao\ufffdt"),
+ (FIELD_TYPE.VAR_STRING, 255, "ao\u00fbt".encode("latin1"), "ao\ufffdt"),
(242, 63, b"\x00\xfb", b"\x00\xfb"),
(FIELD_TYPE.LONG, 63, b"42", 42),
(FIELD_TYPE.NEWDECIMAL, 63, b"1.50", Decimal("1.50")),
@@ -229,7 +231,7 @@ class TestFieldCharsetResult:
result.field_count = 1
result._get_descriptions()
- row = result._read_row_from_packet(MysqlPacket(bytes([len(raw)]) + raw, "utf8")) # type: ignore[attr-defined]
+ row = result._read_row_from_packet(MysqlPacket(bytes([len(raw)]) + raw, "utf8"))
assert row == (expected,)