Conversation
There is a race condition where multiple profiling threads may be spawned. Specifically, if `start_profiling` is called immediately after `stop_profiling`. This happens because `stop_profiling` does not immediately terminate the thread, instead the thread will check that the event was set and exit at the end of the current iteration. If `start_profiling` is called during the iteration, the event gets set again and the old thread will continue running. To fix this, a new event is created when a profiling thread starts so they can be terminated independently.
sentry_sdk/profiler.py
Outdated
| # over the lifetime of the scheduler | ||
| self.event.clear() | ||
| event = threading.Event() | ||
| self.threads.put_nowait(event) |
Member
There was a problem hiding this comment.
just nitpicking but could we possibly have a better name than threads?
Maybe signify purpose, like termination_events/stop_events or something since we use them to break the profiler loops?
sl0thentr0py
approved these changes
Oct 13, 2022
Member
sl0thentr0py
left a comment
There was a problem hiding this comment.
just a naming nitpick, feel free to keep/rename as you deem fit
Zylphrex
added a commit
that referenced
this pull request
Oct 13, 2022
This is fixing a mistake from #1676, and adding a sample at the start of the profile instead of waiting 1 interval before getting the first sample.
Zylphrex
added a commit
that referenced
this pull request
Oct 13, 2022
This is fixing a mistake from #1676, and adding a sample at the start of the profile instead of waiting 1 interval before getting the first sample.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There is a race condition where multiple profiling threads may be spawned. Specifically, if
start_profilingis called immediately afterstop_profiling. This happens becausestop_profilingdoes not immediately terminate the thread, instead the thread will check that the event was set and exit at the end of the current iteration. Ifstart_profilingis called during the iteration, the event gets set again and the old thread will continue running. To fix this, a new event is created when a profiling thread starts so they can be terminated independently.