Skip to content

Conversation

@DCchoudhury15
Copy link

@DCchoudhury15 DCchoudhury15 commented Jan 5, 2026

Description

This PR corrects a logic error in Lib/profiling/sampling/sample.py where _FREE_THREADED_BUILD was incorrectly evaluated as True on Windows non-free-threaded builds when Py_GIL_DISABLED was explicitly set to 0.

The detection has been updated from an is not None check to a bool() cast, which correctly handles:

  • 1 -> True
  • 0 -> False
  • None -> False (standard builds)

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

Tests

  • Verified reproduction by checking that _FREE_THREADED_BUILD returns False when sysconfig.get_config_var("Py_GIL_DISABLED") is 0.
  • Ran the official test suite: ./python -m test test_profiling (Passed).

@python-cla-bot
Copy link

python-cla-bot bot commented Jan 5, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Jan 5, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@DCchoudhury15
Copy link
Author

@pablogsal up for review

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
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
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
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
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.

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
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.

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
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)

@vstinner
Copy link
Member

vstinner commented Jan 8, 2026

cc @pablogsal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants