Skip to content

Commit 5f57f69

Browse files
pythongh-143423: Fix free-threaded build detection in sampling profiler (python#143426)
1 parent 1b08143 commit 5f57f69

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

Lib/profiling/sampling/sample.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def _pause_threads(unwinder, blocking):
4242
LiveStatsCollector = None
4343

4444
_FREE_THREADED_BUILD = sysconfig.get_config_var("Py_GIL_DISABLED") is not None
45+
4546
# Minimum number of samples required before showing the TUI
4647
# If fewer samples are collected, we skip the TUI and just print a message
4748
MIN_SAMPLES_FOR_TUI = 200
@@ -64,19 +65,23 @@ def __init__(self, pid, sample_interval_usec, all_threads, *, mode=PROFILING_MOD
6465
self.realtime_stats = False
6566

6667
def _new_unwinder(self, native, gc, opcodes, skip_non_matching_threads):
67-
if _FREE_THREADED_BUILD:
68-
unwinder = _remote_debugging.RemoteUnwinder(
69-
self.pid, all_threads=self.all_threads, mode=self.mode, native=native, gc=gc,
70-
opcodes=opcodes, skip_non_matching_threads=skip_non_matching_threads,
71-
cache_frames=True, stats=self.collect_stats
72-
)
68+
kwargs = {}
69+
if _FREE_THREADED_BUILD or self.all_threads:
70+
kwargs['all_threads'] = self.all_threads
7371
else:
74-
unwinder = _remote_debugging.RemoteUnwinder(
75-
self.pid, only_active_thread=bool(self.all_threads), mode=self.mode, native=native, gc=gc,
76-
opcodes=opcodes, skip_non_matching_threads=skip_non_matching_threads,
77-
cache_frames=True, stats=self.collect_stats
78-
)
79-
return unwinder
72+
kwargs['only_active_thread'] = bool(self.all_threads)
73+
74+
return _remote_debugging.RemoteUnwinder(
75+
self.pid,
76+
mode=self.mode,
77+
native=native,
78+
gc=gc,
79+
opcodes=opcodes,
80+
skip_non_matching_threads=skip_non_matching_threads,
81+
cache_frames=True,
82+
stats=self.collect_stats,
83+
**kwargs
84+
)
8085

8186
def sample(self, collector, duration_sec=None, *, async_aware=False):
8287
sample_interval_sec = self.sample_interval_usec / 1_000_000
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix free-threaded build detection in the sampling profiler when Py_GIL_DISABLED is set to 0.

0 commit comments

Comments
 (0)