Showing the stack trace from a running Python application
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Capturing stack traces from a live Python process is one of the fastest ways to diagnose hangs, deadlocks, and slow requests. The best method depends on whether you can change code and how much production risk you can tolerate. A strong operational setup includes both in-process and external no-code-change options.
Use faulthandler with Signals
faulthandler is built into Python and is usually the safest first option on Unix-like systems.
Trigger from another shell:
This prints stack traces without stopping the process.
Custom Stack Dumps with sys._current_frames
When you need custom formatting or routing to logs, use a signal handler with thread frame introspection.
This is useful when incident tooling expects structured diagnostic output.
External Inspection with py-spy
If code changes are not possible, py-spy can inspect a running process externally.
Advantages:
- No code modification.
- Useful in production and containers.
- Can show both call stacks and CPU hotspots.
This is often the fastest path during urgent incidents.
Multi-Thread and Deadlock Diagnosis
For deadlock-like behavior, one stack dump is often insufficient. Capture repeated dumps over time and compare frames.
If the same lock-waiting frames repeat for many seconds, you likely have contention or deadlock. If frames change, you may be facing slow I/O or starvation rather than hard deadlock.
Practical strategy:
- Capture three to five dumps with timestamps.
- Correlate with request latency metrics.
- Map blocked threads to application subsystems.
Async Application Considerations
For async services, stack traces should be combined with event-loop diagnostics. A blocked loop thread can freeze many requests while worker threads appear idle.
Add periodic slow-task logging and loop-latency metrics so stack traces have context. Stack dumps alone can show where code is, but not always why throughput dropped.
Operational Workflow
A reliable production workflow:
- Capture process metadata and version.
- Dump stacks multiple times.
- Preserve logs in incident artifact storage.
- Annotate suspected blocking points.
- Reproduce in staging if possible.
Version and deployment metadata are critical for later root-cause analysis.
Security and Privacy
Stack traces can expose secrets, file paths, and internal architecture details. Treat stack dumps as sensitive data:
- Restrict who can trigger dumps.
- Store dumps in protected log channels.
- Redact before sharing broadly.
Diagnostics should not bypass data-governance requirements.
Low-Risk Preparedness Step
Add one documented runbook command per service for stack dumping. During incidents, operators should not invent commands under pressure. A pre-approved procedure with signal name, expected output location, and rollback notes reduces diagnostic delay significantly.
Common Pitfalls
- Taking only one stack snapshot and drawing strong conclusions.
- Enabling overly verbose tracing permanently in production.
- Forgetting thread context and analyzing only main-thread frames.
- Capturing traces without deployment version metadata.
- Relying on one tool only, leaving no fallback when constraints change.
Summary
- Use
faulthandlerplus signals as the default low-friction live stack dump approach. - Use custom handlers when structured or enriched diagnostics are needed.
- Use
py-spywhen code changes are not feasible. - Capture repeated dumps with timestamps for deadlock and hang analysis.
- Treat stack traces as sensitive operational data and control access accordingly.
Related reading

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.