skip to content

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 pageshow

explore

questions

63 · 5 sections

What does Python's built-in breakpoint() do, and how does PYTHONBREAKPOINT control it?

level: juniorimportance: must knowfreq 45%
basics
~20 s

Calling 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.

open as a page

In pdb, how do the step, next, until and return commands differ when stepping?

level: middleimportance: must knowfreq 50%
basics
~10 s

In 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.

open as a page

How do you drop into pdb on the traceback of an exception you have already caught?

level: middleimportance: must knowfreq 45%
basics
~10 s

Call 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.

open as a page

What does `python -m pdb script.py` do when the script dies with an uncaught exception?

level: juniorimportance: should knowfreq 38%
basics
~20 s

Instead 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.

open as a page

Why does Python unbind the name in `except ValueError as exc` when the clause ends?

level: middleimportance: should knowfreq 28%
basics
~20 s

Because 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.

open as a page

What does Python's "coroutine was never awaited" RuntimeWarning mean?

level: juniorimportance: must knowfreq 62%
basics
~20 s

Calling 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.

open as a page

What causes a Python RecursionError, and what do sys.getrecursionlimit and sys.setrecursionlimit control?

level: juniorimportance: must knowfreq 58%
basics
~20 s

CPython 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.

open as a page

Why does a segfault inside a compiled Python extension module leave no traceback?

level: middleimportance: must knowfreq 30%
basics
~20 s

A 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.

open as a page

A Python worker pool renders no more invoices — how do you read an all-threads stack dump to find the stuck frame?

level: seniorimportance: must knowfreq 52%
basics
~10 s

Take 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.

open as a page

What does enabling Python's faulthandler give you when a process dies from a segfault?

level: juniorimportance: should knowfreq 26%
basics
~20 s

It 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.

open as a page

What does returning sys.monitoring.DISABLE from an event callback do?

level: middleimportance: must knowfreq 30%
basics
~20 s

It 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.

open as a page

How does a sampling profiler estimate where a Python program spends its time?

level: middleimportance: must knowfreq 52%
basics
~20 s

A 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.

open as a page

What does returning a function from a sys.settrace 'call' event do?

level: middleimportance: must knowfreq 34%
basics
~20 s

The 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.

open as a page

What three sys.monitoring calls must you make before an event callback fires?

level: juniorimportance: should knowfreq 12%
basics
~10 s

Claim 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.

open as a page

What does sys.addaudithook install in CPython, and which operations raise audit events?

level: middleimportance: should knowfreq 20%
basics
~20 s

sys.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.

open as a page

In a Python traceback, what do the `~~~^^^` marks under the source line point at?

level: juniorimportance: must knowfreq 58%
basics
~10 s

They 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.

open as a page

What is sys.excepthook, and when does CPython call it?

level: juniorimportance: must knowfreq 44%
basics
~20 s

sys.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.

open as a page

What does traceback.format_exc() return, and how does traceback.print_exc() differ?

level: juniorimportance: must knowfreq 68%
basics
~20 s

traceback.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.

open as a page

Why does `str(exc)` on a caught `NameError` omit the "Did you mean" hint the console prints?

level: middleimportance: should knowfreq 34%
basics
~20 s

The 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.

open as a page

Why doesn't sys.excepthook run when a threading.Thread's target raises?

level: middleimportance: should knowfreq 38%
basics
~20 s

A 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.

open as a page

How does warnings.warn() differ from raising an exception in Python?

level: juniorimportance: must knowfreq 45%
basics
~20 s

warnings.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.

open as a page

Why does a DeprecationWarning from an imported library not print by default?

level: middleimportance: must knowfreq 55%
basics
~10 s

Python ships default entries in warnings.filters that ignore DeprecationWarning everywhere except when it is triggered from the main module. Surface them with -W default::DeprecationWarning, PYTHONWARNINGS, or a warnings.filterwarnings call.

open as a page

What does running CPython with the `-X dev` switch turn on?

level: juniorimportance: should knowfreq 30%
basics
~20 s

Development 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.

open as a page

Why does `ResourceWarning: unclosed file` appear only under `-X dev`?

level: middleimportance: should knowfreq 35%
basics
~20 s

ResourceWarning 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.

open as a page

What does the stacklevel argument to warnings.warn() control?

level: middleimportance: should knowfreq 32%
basics
~20 s

stacklevel 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.

open as a page