Skip to content

fix(profiling): Handle potential attribute errors in profiler #2028

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

Merged
Merged
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
79 changes: 44 additions & 35 deletions sentry_sdk/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,7 @@ def extract_stack(
frames = deque(maxlen=max_stack_depth) # type: Deque[FrameType]

while frame is not None:
try:
f_back = frame.f_back
except AttributeError:
capture_internal_exception(sys.exc_info())
# For some reason, the frame we got isn't a `FrameType` and doesn't
# have a `f_back`. When this happens, we continue with any frames
# that we've managed to extract up to this point.
break
f_back = frame.f_back
frames.append(frame)
frame = f_back

Expand Down Expand Up @@ -638,30 +631,35 @@ def write(self, cwd, ts, sample, frame_cache):
elapsed_since_start_ns = str(offset)

for tid, (stack_id, raw_stack, frames) in sample:
# Check if the stack is indexed first, this lets us skip
# indexing frames if it's not necessary
if stack_id not in self.indexed_stacks:
for i, raw_frame in enumerate(raw_stack):
if raw_frame not in self.indexed_frames:
self.indexed_frames[raw_frame] = len(self.indexed_frames)
processed_frame = frame_cache.get(raw_frame)
if processed_frame is None:
processed_frame = extract_frame(frames[i], cwd)
frame_cache[raw_frame] = processed_frame
self.frames.append(processed_frame)

self.indexed_stacks[stack_id] = len(self.indexed_stacks)
self.stacks.append(
[self.indexed_frames[raw_frame] for raw_frame in raw_stack]
try:
# Check if the stack is indexed first, this lets us skip
# indexing frames if it's not necessary
if stack_id not in self.indexed_stacks:
for i, raw_frame in enumerate(raw_stack):
if raw_frame not in self.indexed_frames:
self.indexed_frames[raw_frame] = len(self.indexed_frames)
processed_frame = frame_cache.get(raw_frame)
if processed_frame is None:
processed_frame = extract_frame(frames[i], cwd)
frame_cache[raw_frame] = processed_frame
self.frames.append(processed_frame)

self.indexed_stacks[stack_id] = len(self.indexed_stacks)
self.stacks.append(
[self.indexed_frames[raw_frame] for raw_frame in raw_stack]
)

self.samples.append(
{
"elapsed_since_start_ns": elapsed_since_start_ns,
"thread_id": tid,
"stack_id": self.indexed_stacks[stack_id],
}
)

self.samples.append(
{
"elapsed_since_start_ns": elapsed_since_start_ns,
"thread_id": tid,
"stack_id": self.indexed_stacks[stack_id],
}
)
except AttributeError:
# For some reason, the frame we get doesn't have certain attributes.
# When this happens, we abandon the current sample as it's bad.
capture_internal_exception(sys.exc_info())

def process(self):
# type: () -> ProcessedProfile
Expand Down Expand Up @@ -825,10 +823,21 @@ def _sample_stack(*args, **kwargs):

now = nanosecond_time()

raw_sample = {
tid: extract_stack(frame, last_sample[0].get(tid))
for tid, frame in sys._current_frames().items()
}
try:
raw_sample = {
tid: extract_stack(frame, last_sample[0].get(tid))
for tid, frame in sys._current_frames().items()
}
except AttributeError:
# For some reason, the frame we get doesn't have certain attributes.
# When this happens, we abandon the current sample as it's bad.
capture_internal_exception(sys.exc_info())

# make sure to clear the cache if something went wrong when extracting
# the stack so we dont keep a reference to the last stack of frames around
last_sample[0] = {}

return

# make sure to update the last sample so the cache has
# the most recent stack for better cache hits
Expand Down