Sub topic 15
Limits of pdb and when to use another tool
This chapter assumes you can start a pdb session, step through code, set breakpoints, and use post-mortem mode. It covers where pdb stops being the right tool and what to use then.
Where pdb struggles
pdb is a line-oriented debugger that reads commands from a terminal. Most of its limits follow from that.
- No terminal. A process under gunicorn, a systemd service, or a container without a TTY has nowhere to show the
(Pdb)prompt. The process may hang waiting for input that can never arrive. - Threads and async code. pdb stops the thread that hit the breakpoint, and other threads keep running. Their output interleaves with your prompt. In
asynciocode,nextsteps over anawait, so the event loop runs other tasks while you wait. - Timing bugs. Pausing changes timing. Race conditions and timeouts often disappear while you debug them.
- Speed. Conditional breakpoints run your condition at each hit of that line. A breakpoint in a hot loop with a condition like
i == 99999can slow the program a great deal. - No going back. pdb cannot reverse execution. If you step past the moment of the bug, you must restart.
- Plain interface. There is no variable pane, no mouse, and little syntax highlighting.
Choosing another tool
| Situation | Tool |
|---|---|
| You want tab completion and color at the prompt | `ipdb` or `pdb++` |
| You prefer a graphical stepping interface | The debugger in VS Code or PyCharm |
| The process is remote or has no TTY | `debugpy`, `remote-pdb`, or `web-pdb` |
| The program hangs and you cannot reproduce it under a debugger | `faulthandler` or `py-spy dump` |
| The bug only appears in production | Logging and saved tracebacks |
| You need to see many executions, not one | Logging or `print` |
Python 3.14 added python -m pdb -p PID, which attaches pdb to a running process. Check your version before relying on it.
Swapping in a different debugger
You already know PYTHONBREAKPOINT. It lets you change the debugger without editing code:
pip install ipdb
PYTHONBREAKPOINT=ipdb.set_trace python script.pyEvery breakpoint() call in the script now opens ipdb. The same trick works for any importable callable, which is why you should keep breakpoint() in your code instead of import pdb; pdb.set_trace().
Diagnosing a hang without a debugger
When a program freezes, a stack dump often answers the question faster than a debugging session. The standard library's faulthandler module prints every thread's traceback after a delay:
import faulthandler
import time
faulthandler.dump_traceback_later(3, exit=True)
def wait_forever():
time.sleep(60)
wait_forever()After three seconds, this prints the stack, which points to the time.sleep call inside wait_forever, and exits. For a process you did not prepare in advance, py-spy dump --pid PID gives a similar view from outside.
A decision path
Practical advice
pdb is still the right first choice when you can run the code locally. It needs no installation and works over SSH. Reach for another tool when the problem is where the code runs, how many threads it uses, or how long it takes to fail, not how you step through it.
Exercise
A web service running under gunicorn hangs on some requests. You cannot reproduce the hang locally. Why is adding breakpoint() a poor first step, and what two tools from this chapter would you try instead?
Show answer
Under gunicorn there is no terminal for the (Pdb) prompt, so a breakpoint would block the worker and could make the problem worse. Use py-spy dump --pid PID to see where the stuck worker is, or enable faulthandler (for example dumptracebacklater) to print stack traces. If you need interactive debugging, use a remote debugger such as debugpy or remote-pdb.