While investigating Datasette issue #1268 I found myself with a Python process that was hanging, and I decided to try running gdb against it based on tips in Debugging of CPython processes with gdb
Here's the recipe that worked:
- Find the Docker container ID using
docker ps- in my case it was16197781a7b5 - Attach a new bash shell to that process in privileged mode (needed to get
gdbto work):docker exec --privileged -it 16197781a7b5 bash - Install
gdband the Python tooling for using it:apt-get install gdb python3-dbg - Use
topto find the pid of the running Python process that was hanging. It was20for me. - Run
gdb /usr/bin/python3 -p 20to launchgdbagainst that process - In the
(gdb)prompt runpy-btto see a backtrace.
I'm sure there's lots more that can be done in gdb at this point, but that's how I got to a place where I could interact with the Python process that was running in the Docker container.