From 934046ea210eda81009f0794147d70473a67b04b Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Mon, 7 Sep 2026 11:26:54 +0200 Subject: [PATCH 1/2] fix(redis): Close spans on exception in execute_command --- sentry_sdk/integrations/redis/_async_common.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/sentry_sdk/integrations/redis/_async_common.py b/sentry_sdk/integrations/redis/_async_common.py index b560962875..e3c017ea3d 100644 --- a/sentry_sdk/integrations/redis/_async_common.py +++ b/sentry_sdk/integrations/redis/_async_common.py @@ -152,14 +152,14 @@ async def _sentry_execute_command( set_db_data_fn(db_span, self) _set_client_data(db_span, is_cluster, name, *args) - value = await old_execute_command(self, name, *args, **kwargs) - - db_span.end() - - if cache_span: - _set_cache_data(cache_span, self, cache_properties, value) - cache_span.end() - - return value + try: + value = await old_execute_command(self, name, *args, **kwargs) + return value + finally: + db_span.end() + + if cache_span: + _set_cache_data(cache_span, self, cache_properties, value) + cache_span.end() cls.execute_command = _sentry_execute_command # type: ignore From 9e4cee4ef13185351e65bacd9cd1763b3f0db58c Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Mon, 7 Sep 2026 11:53:39 +0200 Subject: [PATCH 2/2] fix order --- sentry_sdk/integrations/redis/_async_common.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sentry_sdk/integrations/redis/_async_common.py b/sentry_sdk/integrations/redis/_async_common.py index e3c017ea3d..a5db08f982 100644 --- a/sentry_sdk/integrations/redis/_async_common.py +++ b/sentry_sdk/integrations/redis/_async_common.py @@ -154,12 +154,15 @@ async def _sentry_execute_command( try: value = await old_execute_command(self, name, *args, **kwargs) + + if cache_span: + _set_cache_data(cache_span, self, cache_properties, value) + return value finally: db_span.end() if cache_span: - _set_cache_data(cache_span, self, cache_properties, value) cache_span.end() cls.execute_command = _sentry_execute_command # type: ignore