Skip to content

Commit 1566a93

Browse files
fix(profiling): Fix race condition while processing profiles
If multiple threads happen to be processing profiles, we could encounter a race condition, that would lead to more profiles being processed than are available in the `new_profiles` deque. To avoid this, we can break upon encountering an `IndexError`. This line appears to be the most likely cause of #4542 – in any case, handling an `IndexError` is not harmful Fixes #4542
1 parent 9e1cedd commit 1566a93

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

sentry_sdk/profiler/continuous_profiler.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,13 @@ def _sample_stack(*args, **kwargs):
370370
# that are starting profiles are not blocked until it
371371
# can acquire the lock.
372372
for _ in range(new_profiles):
373-
self.active_profiles.add(self.new_profiles.popleft())
373+
try:
374+
profile = self.new_profiles.popleft()
375+
except IndexError:
376+
# If another thread is concurrently popping profiles, we could
377+
# end up with an empty deque here.
378+
break
379+
self.active_profiles.add(profile)
374380
inactive_profiles = []
375381

376382
for profile in self.active_profiles:

0 commit comments

Comments
 (0)