-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-143423: Fix free-threaded build detection in sampling profiler #143426
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
pablogsal
merged 3 commits into
python:main
from
DCchoudhury15:fix-free-threaded-detection
Jan 30, 2026
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -41,7 +41,9 @@ def _pause_threads(unwinder, blocking): | |
| except ImportError: | ||
| LiveStatsCollector = None | ||
|
|
||
| _FREE_THREADED_BUILD = sysconfig.get_config_var("Py_GIL_DISABLED") is not None | ||
| # FIX: Use bool() to correctly detect 0 as False on Windows non-free-threaded builds | ||
| _FREE_THREADED_BUILD = bool(sysconfig.get_config_var("Py_GIL_DISABLED")) | ||
|
|
||
| # Minimum number of samples required before showing the TUI | ||
| # If fewer samples are collected, we skip the TUI and just print a message | ||
| MIN_SAMPLES_FOR_TUI = 200 | ||
|
|
@@ -71,11 +73,19 @@ def _new_unwinder(self, native, gc, opcodes, skip_non_matching_threads): | |
| cache_frames=True, stats=self.collect_stats | ||
| ) | ||
| else: | ||
| unwinder = _remote_debugging.RemoteUnwinder( | ||
| self.pid, only_active_thread=bool(self.all_threads), mode=self.mode, native=native, gc=gc, | ||
| opcodes=opcodes, skip_non_matching_threads=skip_non_matching_threads, | ||
| cache_frames=True, stats=self.collect_stats | ||
| ) | ||
| # FIX: Properly handle all_threads vs only_active_thread parameters | ||
| if self.all_threads: | ||
| unwinder = _remote_debugging.RemoteUnwinder( | ||
| self.pid, all_threads=self.all_threads, mode=self.mode, native=native, gc=gc, | ||
| opcodes=opcodes, skip_non_matching_threads=skip_non_matching_threads, | ||
| cache_frames=True, stats=self.collect_stats | ||
| ) | ||
| else: | ||
| unwinder = _remote_debugging.RemoteUnwinder( | ||
| self.pid, only_active_thread=bool(self.all_threads), mode=self.mode, native=native, gc=gc, | ||
| opcodes=opcodes, skip_non_matching_threads=skip_non_matching_threads, | ||
| cache_frames=True, stats=self.collect_stats | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is almost the same as above. You should do something like: kwargs = {}
if self.all_threads:
kwargs['all_threads' = self.all_threads
unwinder = _remote_debugging.RemoteUnwinder(..., **kwargs) |
||
| ) | ||
| return unwinder | ||
|
|
||
| def sample(self, collector, duration_sec=None, *, async_aware=False): | ||
|
|
||
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2026-01-05-05-31-05.gh-issue-143423.X7YdnR.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 @@ | ||
| Fix free-threaded build detection in the sampling profiler when Py_GIL_DISABLED is set to 0. |
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.
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.
I suggest reverting this change and open a separated PR for it.
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.
@vstinner I attempted to revert the changes to
_new_unwinderas requested, but I found that they are actually required for the tests to pass.If I revert to the original code,
test_profilingfails becauseidle_workerthreads are missing from the stats.The Traceback:
The Logic Error: The issue lies in the original code:
only_active_thread=bool(self.all_threads)When
self.all_threadsis True, this setsonly_active_thread=True.This effectively hides idle threads, which contradicts the intention of all_threads and causes the test failure shown above.It seems my main fix (correcting _FREE_THREADED_BUILD) unmasked this existing logic bug, as the code now enters this path where it previously didn't.
Given this dependency, is it okay to keep the _new_unwinder fix in this PR?
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.
In this case, the
if self.all_threads:change should be merged first. But I still consider that it would be better to have 2 pull requests.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.
@vstinner I have updated the PR as requested.
_new_unwinderlogic to use**kwargs, which removes the code duplication._FREE_THREADED_BUILDdetection fix.This PR now focuses strictly on resolving the thread-handling logic bug. I will submit the build detection fix in a separate PR once this has been addressed.