Live Process Diagnostics
Getting evidence out of a process that is already running, wedged, or dead: debugger attach, thread-stack dumps, interpreter event hooks and error reports. Production bugs rarely reproduce locally.
part ofPythonoverview, primer and where to startread it →on this pageshowhide
explore
- Stopping and Stepping12 questions
- pdb and breakpoint()4 questions
- Post-Mortem Inspection4 questions
- Remote Debugger Attach4 questions
- Crashes and Hangs16 questions
- faulthandler and Fatal Errors3 questions
- Wedged Worker Stack Dumps4 questions
- Aborts From Native Code3 questions
- Memory and Stack Exhaustion3 questions
- Stalled Event Loop3 questions
- Instrumentation Hooks15 questions
- sys.settrace and setprofile4 questions
- sys.monitoring Events3 questions
- Sampling Profilers4 questions
- Audit Events and sys.audit4 questions
- Error Reports12 questions
- Formatting Tracebacks4 questions
- Unhandled Exception Handlers4 questions
- Fine-Grained Source Locations4 questions
- Runtime Check Switches8 questions
- Development Mode3 questions
- Warning Filters5 questions
questions
63 · 5 sectionsWhat does Python's built-in breakpoint() do, and how does PYTHONBREAKPOINT control it?
basics
~20 sCalling breakpoint() pauses the program and drops into a debugger. It dispatches through sys.breakpointhook, which by default calls pdb.set_trace. Setting PYTHONBREAKPOINT=0 makes every call a no-op; setting it to a dotted callable path routes calls there instead.
In pdb, how do the step, next, until and return commands differ when stepping?
basics
~10 sIn pdb, step enters a called function, next runs the call and stays in the current frame, return finishes the current function, and until runs forward past the current line number.
How do you drop into pdb on the traceback of an exception you have already caught?
basics
~10 sCall pdb.post_mortem() with the caught exception's traceback: pdb.post_mortem(exc.__traceback__), or on 3.13+ pass the exception itself. You land in the frame that raised, with its local variables readable but not resumable.
What does `python -m pdb script.py` do when the script dies with an uncaught exception?
basics
~20 sInstead of printing a traceback and exiting, pdb catches the unhandled exception and drops you into post-mortem debugging at the frame that raised, with that frame's local variables still readable. Execution cannot be resumed from the raise point.
Why does Python unbind the name in `except ValueError as exc` when the clause ends?
basics
~20 sBecause the exception references its traceback, which references the frames, which reference the exception - a cycle that would keep the whole stack alive. Python deletes the name at the end of the clause; assign the exception elsewhere first if you need it later.
What does Python's "coroutine was never awaited" RuntimeWarning mean?
basics
~20 sCalling an async def function only builds a coroutine object; none of its body runs until something awaits it or wraps it in a task. When that unstarted object is garbage collected, CPython emits the warning.
What causes a Python RecursionError, and what do sys.getrecursionlimit and sys.setrecursionlimit control?
basics
~20 sCPython counts the Python frames stacked on the current thread and raises RecursionError once that count passes the ceiling sys.getrecursionlimit() reports, which is 1000 on a fresh interpreter. sys.setrecursionlimit() moves the ceiling; it does not enlarge the thread's real stack.
Why does a segfault inside a compiled Python extension module leave no traceback?
basics
~20 sA SIGSEGV kills the process at the operating-system level before CPython can run any Python code, so there is no unwinding, no except clause and no traceback - only a shell status of 139 and possibly a core file.
A Python worker pool renders no more invoices — how do you read an all-threads stack dump to find the stuck frame?
basics
~10 sTake two dumps a few seconds apart. Identical stacks at near-zero CPU means blocked; a pinned core means spinning. Then read each worker's innermost frames and find the one thread that is not waiting.
What does enabling Python's faulthandler give you when a process dies from a segfault?
basics
~20 sIt installs handlers for fatal signals such as SIGSEGV and SIGABRT. When one fires, the interpreter writes a Python stack for each thread to stderr and then lets the process die exactly as it would have.
What does returning sys.monitoring.DISABLE from an event callback do?
basics
~20 sIt retires that one code location for that event and that tool: CPython de-instruments the instruction, so the callback never fires from that spot again and later executions run at almost full speed. sys.monitoring.restart_events re-arms every retired location.
How does a sampling profiler estimate where a Python program spends its time?
basics
~20 sA sampling profiler interrupts the program at a fixed interval, records the current call stack, and counts how often each stack appears. A frame present in 30% of samples held the interpreter for roughly 30% of the profiled period.
What does returning a function from a sys.settrace 'call' event do?
basics
~20 sThe global callback set by sys.settrace fires only on 'call' events, and whatever it returns becomes that frame's local trace function, receiving the frame's 'line', 'return' and 'exception' events. Returning None means that frame is not instrumented further.
What three sys.monitoring calls must you make before an event callback fires?
basics
~10 sClaim a slot with sys.monitoring.use_tool_id, attach a function with sys.monitoring.register_callback, then turn the event on with sys.monitoring.set_events. Skip any one of the three and nothing is delivered; using an unclaimed tool id raises ValueError.
What does sys.addaudithook install in CPython, and which operations raise audit events?
basics
~20 ssys.addaudithook registers a callable that CPython invokes for every audit event, passing the event name and an argument tuple. The runtime raises events at sensitive points: compiling and executing code, importing, opening files, and starting a subprocess.
In a Python traceback, what do the `~~~^^^` marks under the source line point at?
basics
~10 sThey mark the exact sub-expression that failed. Since Python 3.11 (PEP 657) every bytecode instruction carries start and end column offsets, so the traceback underlines the failing operation instead of blaming the whole line.
What is sys.excepthook, and when does CPython call it?
basics
~20 ssys.excepthook is the callback CPython runs when an exception escapes the main thread's top level. The default prints a traceback to sys.stderr and the process exits non-zero. Replace it to log or report the crash.
What does traceback.format_exc() return, and how does traceback.print_exc() differ?
basics
~20 straceback.format_exc() returns the full report for the exception currently being handled as one string. traceback.print_exc() renders the same text but writes it to sys.stderr and returns None. Both read the current exception, so call them inside an except block.
Why does `str(exc)` on a caught `NameError` omit the "Did you mean" hint the console prints?
basics
~20 sThe suggestion is computed when the exception is displayed, not when it is raised. The message string stays "name 'x' is not defined"; the traceback machinery adds the closest matching name it can find in the failing frame.
Why doesn't sys.excepthook run when a threading.Thread's target raises?
basics
~20 sA thread's exception never reaches the main thread's stack, so sys.excepthook is never involved. The threading bootstrap catches it and calls threading.excepthook instead, which prints 'Exception in thread ...' and lets the process carry on with an unchanged exit status.
How does warnings.warn() differ from raising an exception in Python?
basics
~20 swarnings.warn() reports a problem without stopping execution: the call returns and the code keeps running. It also passes through the filter list in warnings.filters first, so the same call may print, be silenced, or be turned into a raised exception.
What does running CPython with the `-X dev` switch turn on?
basics
~20 sDevelopment mode makes a normal interpreter noisy. It adds the default warning filter so ignored categories like ResourceWarning print, installs debug hooks around memory allocations, enables faulthandler and asyncio debug mode, and sets sys.flags.dev_mode to True.
Why does `ResourceWarning: unclosed file` appear only under `-X dev`?
basics
~20 sResourceWarning is discarded by CPython's default warning filters, so nothing prints. Development mode adds the default filter, which shows it. The warning itself is emitted by the file object's finalizer when it is garbage-collected without being closed.
What does the stacklevel argument to warnings.warn() control?
basics
~20 sstacklevel picks which stack frame the warning is blamed on. The default 1 points at the warn() call itself; 2 points at that function's caller, which is what a deprecated API should report so the user sees their own line.