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(data-imports): fail open when redis rejects a row-tracking write (#88650)

Tom Owersca2b952514df
diff --git a/products/warehouse_sources/backend/temporal/data_imports/row_tracking.py b/products/warehouse_sources/backend/temporal/data_imports/row_tracking.py
index 75323c6a9c70..a2f1daf6954f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/row_tracking.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/row_tracking.py
@@ -70,8 +70,14 @@ async def setup_row_tracking(team_id: int, schema_id: uuid.UUID | str) -> None:
if not redis:
return
- await redis.hset(_get_hash_key(team_id), str(schema_id), 0)
- await redis.expire(_get_hash_key(team_id), 60 * 60 * 24 * 7) # 7 day expire
+ try:
+ await redis.hset(_get_hash_key(team_id), str(schema_id), 0)
+ await redis.expire(_get_hash_key(team_id), 60 * 60 * 24 * 7) # 7 day expire
+ except redis_exceptions.RedisError as e:
+ # A successful ping doesn't guarantee later commands succeed (e.g. Redis
+ # refusing writes because it can't persist an RDB snapshot). Row tracking is
+ # best-effort, so a command failing here shouldn't fail the whole import.
+ capture_exception(e)
async def increment_rows(team_id: int, schema_id: uuid.UUID | str, rows: int) -> None:
@@ -79,7 +85,10 @@ async def increment_rows(team_id: int, schema_id: uuid.UUID | str, rows: int) ->
if not redis:
return
- await redis.hincrby(_get_hash_key(team_id), str(schema_id), rows)
+ try:
+ await redis.hincrby(_get_hash_key(team_id), str(schema_id), rows)
+ except redis_exceptions.RedisError as e:
+ capture_exception(e)
async def decrement_rows(team_id: int, schema_id: uuid.UUID | str, rows: int) -> None:
@@ -87,18 +96,21 @@ async def decrement_rows(team_id: int, schema_id: uuid.UUID | str, rows: int) ->
if not redis:
return
- if not await redis.hexists(_get_hash_key(team_id), str(schema_id)):
- return
+ try:
+ if not await redis.hexists(_get_hash_key(team_id), str(schema_id)):
+ return
- value = await redis.hget(_get_hash_key(team_id), str(schema_id))
- if not value:
- return
+ value = await redis.hget(_get_hash_key(team_id), str(schema_id))
+ if not value:
+ return
- value_int = int(value)
- if value_int - rows < 0:
- await redis.hset(_get_hash_key(team_id), str(schema_id), 0)
- else:
- await redis.hincrby(_get_hash_key(team_id), str(schema_id), -rows)
+ value_int = int(value)
+ if value_int - rows < 0:
+ await redis.hset(_get_hash_key(team_id), str(schema_id), 0)
+ else:
+ await redis.hincrby(_get_hash_key(team_id), str(schema_id), -rows)
+ except redis_exceptions.RedisError as e:
+ capture_exception(e)
async def finish_row_tracking(team_id: int, schema_id: uuid.UUID | str) -> None:
@@ -106,7 +118,10 @@ async def finish_row_tracking(team_id: int, schema_id: uuid.UUID | str) -> None:
if not redis:
return
- await redis.hdel(_get_hash_key(team_id), str(schema_id))
+ try:
+ await redis.hdel(_get_hash_key(team_id), str(schema_id))
+ except redis_exceptions.RedisError as e:
+ capture_exception(e)
async def get_rows(team_id: int, schema_id: uuid.UUID | str) -> int:
@@ -114,10 +129,13 @@ async def get_rows(team_id: int, schema_id: uuid.UUID | str) -> int:
if not redis:
return 0
- if await redis.hexists(_get_hash_key(team_id), str(schema_id)):
- value = await redis.hget(_get_hash_key(team_id), str(schema_id))
- if value:
- return int(value)
+ try:
+ if await redis.hexists(_get_hash_key(team_id), str(schema_id)):
+ value = await redis.hget(_get_hash_key(team_id), str(schema_id))
+ if value:
+ return int(value)
+ except redis_exceptions.RedisError as e:
+ capture_exception(e)
return 0
@@ -127,8 +145,12 @@ async def get_all_rows_for_team(team_id: int) -> int:
if not redis:
return 0
- pairs = await redis.hgetall(_get_hash_key(team_id))
- return sum(int(v) for v in pairs.values())
+ try:
+ pairs = await redis.hgetall(_get_hash_key(team_id))
+ return sum(int(v) for v in pairs.values())
+ except redis_exceptions.RedisError as e:
+ capture_exception(e)
+ return 0
# To be removed after 2025-11-06
diff --git a/products/warehouse_sources/backend/temporal/data_imports/tests/test_row_tracking.py b/products/warehouse_sources/backend/temporal/data_imports/tests/test_row_tracking.py
index 9253ead26a7d..ed3febaabf7f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/tests/test_row_tracking.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/tests/test_row_tracking.py
@@ -77,6 +77,32 @@ class TestRowTrackingRedisUnavailable(BaseTest):
unreachable_client.hset.assert_not_called()
mock_capture_exception.assert_not_called()
+ @pytest.mark.asyncio
+ async def test_setup_row_tracking_does_not_raise_when_redis_rejects_writes(self):
+ # A successful ping doesn't guarantee the following command succeeds - e.g. Redis
+ # can refuse writes (MISCONF) if it can't persist an RDB snapshot to disk. That
+ # must fail open like the unreachable-at-ping case above, not crash the sync.
+ read_only_client = mock.AsyncMock()
+ read_only_client.ping.return_value = True
+ read_only_client.hset.side_effect = redis_exceptions.ResponseError(
+ "MISCONF Redis is configured to save RDB snapshots, but it's currently "
+ "unable to persist to disk. Commands that may modify the data set are "
+ "disabled, because this instance is configured to report errors during "
+ "writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). "
+ "Please check the Redis logs for details about the RDB error."
+ )
+
+ with (
+ mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.row_tracking.get_async_client",
+ return_value=read_only_client,
+ ),
+ override_settings(DATA_WAREHOUSE_REDIS_HOST="localhost", DATA_WAREHOUSE_REDIS_PORT="6379"),
+ ):
+ await setup_row_tracking(self.team.pk, str(uuid.uuid4()))
+
+ read_only_client.expire.assert_not_called()
+
@pytest.mark.timeout(600)
@mock.patch(