Skip to content

Don't fail if there is no _context_manager_state #4698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions sentry_sdk/profiler/transaction_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
)
from sentry_sdk.utils import (
capture_internal_exception,
capture_internal_exceptions,
get_current_thread_meta,
is_gevent,
is_valid_sample_rate,
Expand Down Expand Up @@ -369,12 +370,13 @@ def __enter__(self):

def __exit__(self, ty, value, tb):
# type: (Optional[Any], Optional[Any], Optional[Any]) -> None
self.stop()
with capture_internal_exceptions():
self.stop()

scope, old_profile = self._context_manager_state
del self._context_manager_state
scope, old_profile = self._context_manager_state
del self._context_manager_state

scope.profile = old_profile
scope.profile = old_profile
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Context Manager Exception Handling Flaw

The __exit__ method's capture_internal_exceptions() block is overly broad. It silently swallows exceptions from self.stop(), masking legitimate bugs. Additionally, if accessing _context_manager_state fails, the subsequent scope restoration (scope.profile = old_profile) is skipped, leaving the scope referencing a stopped profile and creating an inconsistent state.

Fix in Cursor Fix in Web


def write(self, ts, sample):
# type: (int, ExtractedSample) -> None
Expand Down