diff --git a/Lib/profiling/sampling/sample.py b/Lib/profiling/sampling/sample.py index e73306ebf290e7..f657849e72bb62 100644 --- a/Lib/profiling/sampling/sample.py +++ b/Lib/profiling/sampling/sample.py @@ -42,6 +42,7 @@ def _pause_threads(unwinder, blocking): LiveStatsCollector = None _FREE_THREADED_BUILD = sysconfig.get_config_var("Py_GIL_DISABLED") is not None + # 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 @@ -64,19 +65,23 @@ def __init__(self, pid, sample_interval_usec, all_threads, *, mode=PROFILING_MOD self.realtime_stats = False def _new_unwinder(self, native, gc, opcodes, skip_non_matching_threads): - if _FREE_THREADED_BUILD: - 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 - ) + kwargs = {} + if _FREE_THREADED_BUILD or self.all_threads: + kwargs['all_threads'] = self.all_threads 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 - ) - return unwinder + kwargs['only_active_thread'] = bool(self.all_threads) + + return _remote_debugging.RemoteUnwinder( + self.pid, + mode=self.mode, + native=native, + gc=gc, + opcodes=opcodes, + skip_non_matching_threads=skip_non_matching_threads, + cache_frames=True, + stats=self.collect_stats, + **kwargs + ) def sample(self, collector, duration_sec=None, *, async_aware=False): sample_interval_sec = self.sample_interval_usec / 1_000_000 diff --git a/Misc/NEWS.d/next/Library/2026-01-05-05-31-05.gh-issue-143423.X7YdnR.rst b/Misc/NEWS.d/next/Library/2026-01-05-05-31-05.gh-issue-143423.X7YdnR.rst new file mode 100644 index 00000000000000..d9276dfd400a5d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-01-05-05-31-05.gh-issue-143423.X7YdnR.rst @@ -0,0 +1 @@ +Fix free-threaded build detection in the sampling profiler when Py_GIL_DISABLED is set to 0.