@@ -905,3 +905,67 @@ def test_last_event_id_cleared(sentry_init):
905905 Scope .get_isolation_scope ().clear ()
906906
907907 assert Scope .last_event_id () is None , "last_event_id should be cleared"
908+
909+
910+ @pytest .mark .tests_internal_exceptions
911+ @pytest .mark .parametrize (
912+ "scope_manager" ,
913+ [
914+ new_scope ,
915+ use_scope ,
916+ ],
917+ )
918+ def test_handle_lookup_error_on_token_reset_current_scope (scope_manager ):
919+ with mock .patch ("sentry_sdk.scope.capture_internal_exception" ) as mock_capture :
920+ with mock .patch ("sentry_sdk.scope._current_scope" ) as mock_token_var :
921+ mock_token_var .reset .side_effect = LookupError ()
922+
923+ mock_token = mock .Mock ()
924+ mock_token_var .set .return_value = mock_token
925+
926+ try :
927+ if scope_manager == use_scope :
928+ with scope_manager (Scope ()):
929+ pass
930+ else :
931+ with scope_manager ():
932+ pass
933+
934+ except Exception :
935+ pytest .fail ("Context manager should handle LookupError gracefully" )
936+
937+ mock_capture .assert_called_once ()
938+ mock_token_var .reset .assert_called_once_with (mock_token )
939+
940+
941+ @pytest .mark .tests_internal_exceptions
942+ @pytest .mark .parametrize (
943+ "scope_manager" ,
944+ [
945+ isolation_scope ,
946+ use_isolation_scope ,
947+ ],
948+ )
949+ def test_handle_lookup_error_on_token_reset_isolation_scope (scope_manager ):
950+ with mock .patch ("sentry_sdk.scope.capture_internal_exception" ) as mock_capture :
951+ with mock .patch ("sentry_sdk.scope._current_scope" ) as mock_current_scope :
952+ with mock .patch (
953+ "sentry_sdk.scope._isolation_scope"
954+ ) as mock_isolation_scope :
955+ mock_isolation_scope .reset .side_effect = LookupError ()
956+ mock_current_token = mock .Mock ()
957+ mock_current_scope .set .return_value = mock_current_token
958+
959+ try :
960+ if scope_manager == use_isolation_scope :
961+ with scope_manager (Scope ()):
962+ pass
963+ else :
964+ with scope_manager ():
965+ pass
966+
967+ except Exception :
968+ pytest .fail ("Context manager should handle LookupError gracefully" )
969+
970+ mock_capture .assert_called_once ()
971+ mock_current_scope .reset .assert_called_once_with (mock_current_token )
0 commit comments