Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions Lib/profiling/sampling/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, I have updated _new_unwinder to properly handle the all_threads vs only_active_thread parameters based on the detected build type.

I suggest reverting this change and open a separated PR for it.

Copy link
Copy Markdown
Contributor Author

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_unwinder as requested, but I found that they are actually required for the tests to pass.

If I revert to the original code, test_profiling fails because idle_worker threads are missing from the stats.

The Traceback:

test test_profiling failed -- Traceback (most recent call last):
  File "/workspaces/cpython/Lib/test/test_profiling/test_sampling_profiler/test_modes.py", line 197, in test_cpu_mode_integration_filtering
    self.assertIn("idle_worker", wall_mode_output)
AssertionError: 'idle_worker' not found in 'Captured 401 samples...

The Logic Error: The issue lies in the original code: only_active_thread=bool(self.all_threads)

When self.all_threads is True, this sets only_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?

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

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.

  1. Refactored the _new_unwinder logic to use **kwargs, which removes the code duplication.
  2. Reverted the _FREE_THREADED_BUILD detection 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.

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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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):
Expand Down
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.
Loading