FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-148932: Fix `profiling.sampling` on Windows virtual environments by edvilme · Pull Request #150541 · python/cpython · GitHub

Repository navigation

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

chris-eibl merged 8 commits into
python:mainfrom
edvilme:profiling-windows-venv

Conversation

edvilme commented May 27, 2026 •
edited
Loading

Copy link
Copy Markdown
Contributor

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 Bot commented May 27, 2026 •
edited
Loading

Copy link
Copy Markdown

Documentation build overview

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

  🔍 Preview build  

105 files changed · + 1 added · ± 104 modified

+ Added

± Modified

chris-eibl left a comment

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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 commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

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

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

pablogsal left a comment

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM! Great work @edvilme

Copy link
Copy Markdown
Member

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

edvilme commented Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

Renamed method to _resolve_python_pid
CC @pablogsal

chris-eibl enabled auto-merge (squash) June 8, 2026 17:05
chris-eibl added the needs backport to 3.15 bugs and security fixes label Jun 8, 2026
chris-eibl merged commit 5c13217 into python:main Jun 8, 2026
54 checks passed

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 Bot commented Jun 8, 2026

Copy link
Copy Markdown

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

bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Jun 8, 2026
pablogsal pushed a commit that referenced this pull request Jun 8, 2026
…ments (GH-150541) (#151097)

gh-148932: Fix `profiling.sampling` on Windows virtual environments (GH-150541)
(cherry picked from commit 5c13217)

Co-authored-by: Eduardo Villalpando Mello <eduardovil@microsoft.com>
philthompson10 pushed a commit to philthompson10/cpython that referenced this pull request Jun 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL