Skip to content

_remote_debugging: --mode cpu --blocking can return an empty profile #162427

Open

Description

@maurycy

Bug report

The simplest repro:

maurycy@gimel cpython (main e0d0f2e?) % sudo ./python.exe -m profiling.sampling run --mode cpu --blocking -m timeit
100000000 loops, best of 5: 3.15 nsec per loop
Captured 2,185 samples in 2.19 seconds
Sample rate: 999.95 samples/sec
Error rate: 0.00
No samples were collected.
This can happen in CPU mode when all threads are idle.

Note the No samples were collected..

A bit more complex repro but the result is the asme:

2026-11-22T11:09:05.918638000+0100 maurycy@gimel /Users/maurycy/src/github.com/maurycy/cpython (main e0d0f2e?) % echo 'print(sum(i*i for i in range(10_000_000)))' > busy.py
2026-11-22T11:09:06.849092000+0100 maurycy@gimel /Users/maurycy/src/github.com/maurycy/cpython (main e0d0f2e?) % sudo ./python.exe -m profiling.sampling run --mode cpu --blocking busy.py
333333283333335000000
Captured 343 samples in 0.34 seconds
Sample rate: 999.63 samples/sec
Error rate: 0.29
No samples were collected.
This can happen in CPU mode when all threads are idle.

The cause: we task_suspend() before doing get_thread_status() and we never get TH_STATE_RUNNING so cannot report ON_CPU. I suspect the same thing happens on Linux

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Activity

  1. maurycy commented on Nov 22, 2026

    @maurycy
    ContributorAuthor

    cc @pablogsal I did not explore the solution space here. The obvious intuition is to gather the state before pausing. It feels racy, though.

  2. pablogsal commented on Nov 22, 2026

    @pablogsal
    Member

    I see this on Linux too. A busy thread goes from R to t when we pause it, so the CPU filter drops it.

    I think your suggestion is right. We want the CPU state to tell us what the thread was doing when we sampled it. Once we stop the thread, that state reflects our own pause. I suggest we save the state by native thread ID just before the pause, then read the stacks once the threads stop. A thread can still go idle in between, but the pause itself will no longer make every busy thread disappear from the profile.

  3. pablogsal commented on Nov 22, 2026

    @pablogsal
    Member

    Do you want to work on a fix?

  4. maurycy commented on Nov 22, 2026

    @maurycy
    ContributorAuthor

    @pablogsal Yes, but... there's some backlog I want to go through first, especially all the great code reviews! Realistically I'd put this on hold for a week or two. Maybe we can pair, just like in #161956?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions