From 66da7091c77bb9f803b0e7365d3ae4c98bf0a784 Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Wed, 15 Oct 2025 14:58:45 +0200 Subject: [PATCH] Handle ValueError in scope resets --- sentry_sdk/scope.py | 12 ++++++------ tests/test_scope.py | 14 ++++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index c871e6a467..f9caf7e1d6 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -1679,7 +1679,7 @@ def new_scope(): try: # restore original scope _current_scope.reset(token) - except LookupError: + except (LookupError, ValueError): capture_internal_exception(sys.exc_info()) @@ -1717,7 +1717,7 @@ def use_scope(scope): try: # restore original scope _current_scope.reset(token) - except LookupError: + except (LookupError, ValueError): capture_internal_exception(sys.exc_info()) @@ -1761,12 +1761,12 @@ def isolation_scope(): # restore original scopes try: _current_scope.reset(current_token) - except LookupError: + except (LookupError, ValueError): capture_internal_exception(sys.exc_info()) try: _isolation_scope.reset(isolation_token) - except LookupError: + except (LookupError, ValueError): capture_internal_exception(sys.exc_info()) @@ -1808,12 +1808,12 @@ def use_isolation_scope(isolation_scope): # restore original scopes try: _current_scope.reset(current_token) - except LookupError: + except (LookupError, ValueError): capture_internal_exception(sys.exc_info()) try: _isolation_scope.reset(isolation_token) - except LookupError: + except (LookupError, ValueError): capture_internal_exception(sys.exc_info()) diff --git a/tests/test_scope.py b/tests/test_scope.py index e645d84234..68c93f3036 100644 --- a/tests/test_scope.py +++ b/tests/test_scope.py @@ -908,6 +908,7 @@ def test_last_event_id_cleared(sentry_init): @pytest.mark.tests_internal_exceptions +@pytest.mark.parametrize("error_cls", [LookupError, ValueError]) @pytest.mark.parametrize( "scope_manager", [ @@ -915,10 +916,10 @@ def test_last_event_id_cleared(sentry_init): use_scope, ], ) -def test_handle_lookup_error_on_token_reset_current_scope(scope_manager): +def test_handle_error_on_token_reset_current_scope(error_cls, scope_manager): with mock.patch("sentry_sdk.scope.capture_internal_exception") as mock_capture: with mock.patch("sentry_sdk.scope._current_scope") as mock_token_var: - mock_token_var.reset.side_effect = LookupError() + mock_token_var.reset.side_effect = error_cls() mock_token = mock.Mock() mock_token_var.set.return_value = mock_token @@ -932,13 +933,14 @@ def test_handle_lookup_error_on_token_reset_current_scope(scope_manager): pass except Exception: - pytest.fail("Context manager should handle LookupError gracefully") + pytest.fail(f"Context manager should handle {error_cls} gracefully") mock_capture.assert_called_once() mock_token_var.reset.assert_called_once_with(mock_token) @pytest.mark.tests_internal_exceptions +@pytest.mark.parametrize("error_cls", [LookupError, ValueError]) @pytest.mark.parametrize( "scope_manager", [ @@ -946,13 +948,13 @@ def test_handle_lookup_error_on_token_reset_current_scope(scope_manager): use_isolation_scope, ], ) -def test_handle_lookup_error_on_token_reset_isolation_scope(scope_manager): +def test_handle_error_on_token_reset_isolation_scope(error_cls, scope_manager): with mock.patch("sentry_sdk.scope.capture_internal_exception") as mock_capture: with mock.patch("sentry_sdk.scope._current_scope") as mock_current_scope: with mock.patch( "sentry_sdk.scope._isolation_scope" ) as mock_isolation_scope: - mock_isolation_scope.reset.side_effect = LookupError() + mock_isolation_scope.reset.side_effect = error_cls() mock_current_token = mock.Mock() mock_current_scope.set.return_value = mock_current_token @@ -965,7 +967,7 @@ def test_handle_lookup_error_on_token_reset_isolation_scope(scope_manager): pass except Exception: - pytest.fail("Context manager should handle LookupError gracefully") + pytest.fail(f"Context manager should handle {error_cls} gracefully") mock_capture.assert_called_once() mock_current_scope.reset.assert_called_once_with(mock_current_token)