-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
gh-116738: Make cProfile module thread-safe #138229
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
+100
−14
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3bb0e14
gh-116738: Make cProfile module thread-safe
yoney d4d4d51
gh-116738: Remove the unnecessary critical_section parameter
yoney 42942c5
gh-116738: Remove the critical_section for profiler_init()
yoney 0f0a658
Merge branch 'main' into ft_cprofile
kumaraditya303 e6eeb1e
Update Misc/NEWS.d/next/Core_and_Builtins/2025-08-28-09-29-46.gh-issu…
kumaraditya303 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import unittest | ||
|
||
from test.support import threading_helper | ||
|
||
import cProfile | ||
import pstats | ||
|
||
|
||
NTHREADS = 10 | ||
INSERT_PER_THREAD = 1000 | ||
|
||
|
||
@threading_helper.requires_working_threading() | ||
class TestCProfile(unittest.TestCase): | ||
def test_cprofile_racing_list_insert(self): | ||
def list_insert(lst): | ||
for i in range(INSERT_PER_THREAD): | ||
lst.insert(0, i) | ||
|
||
lst = [] | ||
|
||
with cProfile.Profile() as pr: | ||
threading_helper.run_concurrently( | ||
worker_func=list_insert, nthreads=NTHREADS, args=(lst,) | ||
) | ||
pr.create_stats() | ||
ps = pstats.Stats(pr) | ||
stats_profile = ps.get_stats_profile() | ||
list_insert_profile = stats_profile.func_profiles[ | ||
"<method 'insert' of 'list' objects>" | ||
] | ||
# Even though there is no explicit recursive call to insert, | ||
# cProfile may record some calls as recursive due to limitations | ||
# in its handling of multithreaded programs. This issue is not | ||
# directly related to FT Python itself; however, it tends to be | ||
# more noticeable when using FT Python. Therefore, consider only | ||
# the calls section and disregard the recursive part. | ||
list_insert_ncalls = list_insert_profile.ncalls.split("/")[0] | ||
self.assertEqual( | ||
int(list_insert_ncalls), NTHREADS * INSERT_PER_THREAD | ||
) | ||
|
||
self.assertEqual(len(lst), NTHREADS * INSERT_PER_THREAD) |
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Core_and_Builtins/2025-08-28-09-29-46.gh-issue-116738.yLZJpV.rst
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Make :mod:`cProfile` thread-safe on the :term:`free threaded <free | ||
threading>` build | ||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.