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
fix(profiling): Race condition spawning multiple profiling threads #1676
Merged
Zylphrex
merged 11 commits into
master
from
txiao/fix/race-condition-spawning-multiple-profiler-threads
Oct 13, 2022
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c4642e9
fix(profiling): Race condition spawning multiple profiling threads
Zylphrex 8840f72
run black
Zylphrex 32770b6
run flake8
Zylphrex 6364826
run black
Zylphrex 72abea4
fix thread init
Zylphrex 4da1347
only run on python3.3+
Zylphrex 2e4d899
cleanup decorator
Zylphrex 348a642
update minimum python version message
Zylphrex 1436631
sleep less
Zylphrex 967c05f
Merge branch 'master' into txiao/fix/race-condition-spawning-multiple…
Zylphrex a833a78
rename threads to stop_events
Zylphrex File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import sys | ||
import threading | ||
import time | ||
|
||
import pytest | ||
|
||
from sentry_sdk.profiler import SleepScheduler | ||
|
||
|
||
minimum_python_33 = pytest.mark.skipif( | ||
sys.version_info < (3, 3), reason="Profiling is only supported in Python >= 3.3" | ||
) | ||
|
||
|
||
def get_scheduler_threads(scheduler): | ||
return [thread for thread in threading.enumerate() if thread.name == scheduler.name] | ||
|
||
|
||
@minimum_python_33 | ||
def test_sleep_scheduler_single_background_thread(): | ||
def sampler(): | ||
pass | ||
|
||
scheduler = SleepScheduler(sampler=sampler, frequency=1000) | ||
|
||
assert scheduler.start_profiling() | ||
|
||
# the scheduler thread does not immediately exit | ||
# but it should exit after the next time it samples | ||
assert scheduler.stop_profiling() | ||
|
||
assert scheduler.start_profiling() | ||
|
||
# because the scheduler thread does not immediately exit | ||
# after stop_profiling is called, we have to wait a little | ||
# otherwise, we'll see an extra scheduler thread in the | ||
# following assertion | ||
# | ||
# one iteration of the scheduler takes 1.0 / frequency seconds | ||
# so make sure this sleeps for longer than that to avoid flakes | ||
time.sleep(0.002) | ||
|
||
# there should be 1 scheduler thread now because the first | ||
# one should be stopped and a new one started | ||
assert len(get_scheduler_threads(scheduler)) == 1 | ||
|
||
assert scheduler.stop_profiling() | ||
|
||
# because the scheduler thread does not immediately exit | ||
# after stop_profiling is called, we have to wait a little | ||
# otherwise, we'll see an extra scheduler thread in the | ||
# following assertion | ||
# | ||
# one iteration of the scheduler takes 1.0 / frequency seconds | ||
# so make sure this sleeps for longer than that to avoid flakes | ||
time.sleep(0.002) | ||
|
||
# there should be 0 scheduler threads now because they stopped | ||
assert len(get_scheduler_threads(scheduler)) == 0 |
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 was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?