From 5f57f6970bdea45cc2b192d612bfd8199f51491e Mon Sep 17 00:00:00 2001 From: Divyanshu Choudhury Date: Fri, 30 Jan 2026 06:05:30 +0530 Subject: [PATCH] gh-143423: Fix free-threaded build detection in sampling profiler (#143426) --- Lib/profiling/sampling/sample.py | 29 +++++++++++-------- ...-01-05-05-31-05.gh-issue-143423.X7YdnR.rst | 1 + 2 files changed, 18 insertions(+), 12 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-01-05-05-31-05.gh-issue-143423.X7YdnR.rst diff --git a/Lib/profiling/sampling/sample.py b/Lib/profiling/sampling/sample.py index e73306ebf290..f657849e72bb 100644 --- a/Lib/profiling/sampling/sample.py +++ b/Lib/profiling/sampling/sample.py @@ -42,6 +42,7 @@ except ImportError: 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 @@ class SampleProfiler: 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 000000000000..d9276dfd400a --- /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. -- 2.47.3