Skip to content

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

Merged
chris-eibl merged 8 commits into
python:mainfrom
edvilme:profiling-windows-venv
Jun 8, 2026
Merged

gh-148932: Fix profiling.sampling on Windows virtual environments#150541
chris-eibl merged 8 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 #33044001 | 📁 Comparing c71b3ca against main (9242700)

  🔍 Preview build  

105 files changed · + 1 added · ± 104 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

@chris-eibl chris-eibl enabled auto-merge (squash) June 8, 2026 17:05
@chris-eibl chris-eibl added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 8, 2026
@chris-eibl chris-eibl merged commit 5c13217 into python:main Jun 8, 2026
54 checks passed
@miss-islington-app
Copy link
Copy Markdown

Thanks @edvilme for the PR, and @chris-eibl for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented Jun 8, 2026

GH-151097 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants