Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
kumaraditya303 merged 5 commits intopython:mainfromyoney:ft_cprofile
Sep 6, 2025

Conversation

@yoney
Copy link
Contributor

@yoneyyoney commentedAug 28, 2025
edited by bedevere-appbot
Loading

This change uses critical sections to makecProfile (_lsprof) thread-safe when the GIL is disabled. It does this by applying the@critical_section clinic directive toProfilerObject.

ProtectingProfilerObject prevents crashes, and the thread sanitizer does not report errors for the new FT test. However, the stats still show incorrect recursive call information.

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)10000/538    0.085    0.000    0.186    0.000 {method 'insert' of 'list' objects}

This happens because of limitations in thecProfile module, and these issues are not directly related to the FT Python build. By adjustingsys.setswitchinterval(), you can see similar incorrect recursive call reports even in a non-FT Python build.

This is a quick fix to prevent crashes. It is also a request for comments on whether we should make bigger changes to thecProfile module to handle multithreading better, such as collecting profile data per thread.

cc:@mpage,@colesbury,@Yhg1s

@mpage
Copy link
Contributor

@yoney - LGTM. I think it's worth doing the work to makecProfile work well in a multi-threaded environment in the free-threaded builds. Do you have a sense of the scope of the changes that will be required to makecProfile work correctly in a multi-threaded setting? Unless they're similar in scope to these changes I think it's probably worth merging this PR (and requesting a backport to 3.14) since it shouldn't introduce too much additional churn.

yoney reacted with thumbs up emoji

@yoney
Copy link
ContributorAuthor

@yoney - LGTM. I think it's worth doing the work to makecProfile work well in a multi-threaded environment in the free-threaded builds. Do you have a sense of the scope of the changes that will be required to makecProfile work correctly in a multi-threaded setting? Unless they're similar in scope to these changes I think it's probably worth merging this PR (and requesting a backport to 3.14) since it shouldn't introduce too much additional churn.

@mpage Thanks for the review! I think collecting profiling data per thread and merging it lazily before reporting could work. This kind of refactoring would likely benefitsubinterpreters too. I’m happy to continue working on those changes as a follow-up, but I agree that the scope would be different from this PR.

For now, this PR addresses the immediate issue that causes crashes. While the recursive-count might still be incorrect, the other counts like call-count are accurate, and this fix resolves the crash. I think it makes sense to land this now and consider the follow-up separately.

@kumaraditya303kumaraditya303 added the needs backport to 3.14bugs and security fixes labelSep 6, 2025
@kumaraditya303kumaraditya303 merged commit8554c09 intopython:mainSep 6, 2025
47 checks passed
@miss-islington-app
Copy link

Thanks@yoney for the PR, and@kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestSep 6, 2025
(cherry picked from commit8554c09)Co-authored-by: Alper <alperyoney@fb.com>Co-authored-by: Kumar Aditya <kumaraditya@python.org>
@bedevere-app
Copy link

GH-138575 is a backport of this pull request to the3.14 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.14bugs and security fixes labelSep 6, 2025
lkollar pushed a commit to lkollar/cpython that referenced this pull requestSep 9, 2025
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
kumaraditya303 added a commit that referenced this pull requestOct 7, 2025
…8575)gh-116738: make `cProfile` module thread-safe (GH-138229)(cherry picked from commit8554c09)Co-authored-by: Alper <alperyoney@fb.com>Co-authored-by: Kumar Aditya <kumaraditya@python.org>
@yoneyyoney deleted the ft_cprofile branchOctober 19, 2025 03:02
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@kumaraditya303kumaraditya303kumaraditya303 approved these changes

@mpagempageAwaiting requested review from mpage

@colesburycolesburyAwaiting requested review from colesbury

@Yhg1sYhg1sAwaiting requested review from Yhg1s

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@yoney@mpage@kumaraditya303

[8]ページ先頭

©2009-2025 Movatter.jp