pyheap icon indicating copy to clipboard operation
pyheap copied to clipboard

Tool is hanging, no result is produced

Open dropkick4124 opened this issue 1 year ago • 3 comments

Hi @ivanyu !

I am trying to dump the heap from the following test program :

import time
print("hello")
time.sleep(5000)

The tool runs correctly with python3 pyheap_dump.py --pid [PID] --file heap.pyheap, however it hangs indefinitely here :

Breakpoint 1 at 0xXXXXXXXXXX: file Python/ceval.c, line 921.
Continuing.

I tested multiple Python3 versions, even the docker implementation. The progress.json is always empty, and never gets updated. Any help ?

Thanks !

dropkick4124 avatar Feb 24 '24 19:02 dropkick4124

Same Problem. Have you finally solved?

ssweiplus avatar Mar 28 '25 03:03 ssweiplus

I'll have a look

ivanyu avatar Apr 25 '25 05:04 ivanyu

The root cause is twofold.

When we call time.sleep(5000), down below, CPython calls nanosleep, which effectively stops the execution of the main (in this case) thread until the timer expires. Since it's the only thread, GDB effectively has to wait for 5000 seconds until it has a chance to reach the set breakpoint and then dump the heap.

In real programs we rarely wait that long uninterrupted. If we do something like

import time
print("hello")
for i in range(5000):
    time.sleep(i)

PyHeap should do better.

However, another issue manifests after that. Sometimes the stack frame evaluation is optimized out and the breakpoint isn't hit. To prevent this, we can do something like:

import time
print("hello")
for i in range(5000):
    time.sleep(i)
    eval("123")  # prevent frame optimizations

The former cause is probably unavoidable, but probably quite rare in real apps. The latter could be worked around in the PyHeap code, I'll do this at some point.

ivanyu avatar Apr 26 '25 04:04 ivanyu