Skip to content

gh-148932: Fix profiling.sampling on Windows virtual environments#150541

Open
edvilme wants to merge 7 commits into
python:mainfrom
edvilme:profiling-windows-venv
Open

gh-148932: Fix profiling.sampling on Windows virtual environments#150541
edvilme wants to merge 7 commits into
python:mainfrom
edvilme:profiling-windows-venv

Conversation

@edvilme
Copy link
Copy Markdown
Contributor

@edvilme edvilme commented May 27, 2026

Currently, it is not possible to run profiling.sampling from a Windows venv because virtual environments use shims to launch the original interpreter, but the shim itself isn't a real Python process.

Fix

Added _resolve_venv_child function to detect when the profiler is started from a virtual environment process on Windows, and automatically redirect it to the real child Python process. This prevents profiling against the shim instead of the actual Python process.

The SampleProfiler class uses the resolved child PID when initializing.

Before

(venv) PS C:\Users\user\Documents\project> python -m profiling.sampling run .\test.py
Running profiling.sampling from virtualenv on Windows platform is not supported

After

(venv) PS C:\Users\user\Documents\project> python -m profiling.sampling run .\test.py
Profile Stats:
       nsamples   sample%  tottime (ms)    cumul%  cumtime (ms)  filename:lineno(function)
        465/465      24.6       465.000      24.6       465.000  test.py:59(bubble_sort)
        452/589      24.0       452.000      31.2       589.000  test.py:27(fibonacci_recursive)
        400/400      21.2       400.000      21.2       400.000  test.py:60(bubble_sort)
        227/227      12.0       227.000      12.0       227.000  test.py:72(matrix_multiply)
        130/130       6.9       130.000       6.9       130.000  test.py:58(bubble_sort)
         95/123       5.0        95.000       6.5       123.000  test.py:25(fibonacci_recursive)
          27/27       1.4        27.000       1.4        27.000  test.py:71(matrix_multiply)
          23/23       1.2        23.000       1.2        23.000  test.py:26(fibonacci_recursive)
          19/31       1.0        19.000       1.6        31.000  test.py:23(fibonacci_recursive)
            9/9       0.5         9.000       0.5         9.000  test.py:34(is_prime)
            7/7       0.4         7.000       0.4         7.000  test.py:35(is_prime)
            6/6       0.3         6.000       0.3         6.000  test.py:84(compute_collatz)
            4/8       0.2         4.000       0.4         8.000  random.py:342(Random.randint)
            3/3       0.2         3.000       0.2         3.000  random.py:339(Random.randint)
            3/3       0.2         3.000       0.2         3.000  test.py:80(compute_collatz)

Legend:
  nsamples: Direct/Cumulative samples (direct executing / on call stack)
  sample%: Percentage of total samples this function was directly executing
  tottime: Estimated total time spent directly in this function
  cumul%: Percentage of total samples when this function was on the call stack
  cumtime: Estimated cumulative time (including time in called functions)
  filename:lineno(function): Function location and name

CC @pablogsal

@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community Bot commented May 27, 2026

Documentation build overview

📚 cpython-previews | 🛠️ Build #32999827 | 📁 Comparing deb4711 against main (9242700)

  🔍 Preview build  

98 files changed · + 1 added · ± 97 modified

+ Added

± Modified

Copy link
Copy Markdown
Member

@chris-eibl chris-eibl left a comment

Choose a reason for hiding this comment

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

I've just verified locally that it works in a virtual environment now, so approving.

Thanks for your contribution @edvilme!

I'd even backport to 3.15 (rather a bug fix than a feature IMO), but leave the final decision to @pablogsal.

@edvilme
Copy link
Copy Markdown
Contributor Author

edvilme commented Jun 4, 2026

Hi @chris-eibl thank you!!
I agree it is a bug fix, should I do anything for the backport?

@chris-eibl
Copy link
Copy Markdown
Member

No, thanks. Let's wait for Pablo's decision first, and then most probably bedevere-bot can take care of it, anyway.

Comment thread Lib/profiling/sampling/sample.py Outdated
Copy link
Copy Markdown
Member

@pablogsal pablogsal left a comment

Choose a reason for hiding this comment

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

LGTM! Great work @edvilme

@pablogsal
Copy link
Copy Markdown
Member

Please address that nit and ping me again so I or @chris-eibl can land

@edvilme
Copy link
Copy Markdown
Contributor Author

edvilme commented Jun 5, 2026

Renamed method to _resolve_python_pid
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.

3 participants