pyheap
pyheap copied to clipboard
Tool is hanging, no result is produced
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 !
Same Problem. Have you finally solved?
I'll have a look
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.