Find potential deadlocks in concurrent code
✓Works with OpenClaudeYou are a concurrent programming debugger. The user wants to find potential deadlocks in concurrent code by analyzing lock acquisition patterns, thread interactions, and circular wait conditions.
What to check first
- Run
jps -l(Java) orps aux | grep javato identify running processes with threads - Check thread dump:
jstack <pid>to see current lock holders and waiters - Review code for nested locks, shared resources, and lock ordering inconsistencies
Steps
- Capture a thread dump using
jstack <pid> > threadump.txtand search forwaiting to lockpatterns that form cycles - Analyze lock acquisition order across all threads—deadlock requires circular dependencies (Thread A holds lock X waiting for Y, Thread B holds Y waiting for X)
- Use
ThreadMXBeanto programmatically detect deadlocked threads: callThreadMXBean.findDeadlockedThreads()orfindMonitorDeadlockedThreads() - Instrument code with lock ordering verification—assign a numeric priority to each lock and enforce that locks are always acquired in ascending order
- Enable
-XX:+PrintConcurrentLocksJVM flag to print lock information in thread dumps for clearer analysis - Build a lock acquisition graph: map which threads hold which locks and which locks each thread is waiting for
- Check for common patterns: nested synchronized blocks on different objects, callback methods acquiring locks, condition variables without proper signaling
- Run static analysis tools like Checker Framework or ThreadSanitizer (for C/C++) to detect potential data races and lock issues before runtime
Code
import java.lang.management.ThreadMXBean;
import java.lang.management.ManagementFactory;
public class DeadlockDetector {
private static final ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
// Lock ordering enforcement
private static class LockTracker {
private static final ThreadLocal<java.util.TreeSet<Integer>> lockStack =
ThreadLocal.withInitial(java.util.TreeSet::new);
public static void acquireLock(int lockId) {
java.util.TreeSet<Integer> stack = lockStack.get();
if (!stack.isEmpty() && stack.last() > lockId) {
throw new IllegalStateException(
"Lock ordering violation: attempting to acquire lock " + lockId +
" while holding higher-priority lock " + stack.last());
}
stack.add(lockId);
}
public static void releaseLock(int lockId) {
lockStack.get().remove(lockId);
}
}
// Periodic deadlock detection
public static void detectDeadlocks() {
long[] deadlockedThreads = threadBean.findDeadlockedThreads();
if (deadlockedThreads != null && deadlockedThreads.length > 0) {
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Debugging Skills
Other Claude Code skills in the same category — free to download.
Error Analyzer
Analyze error messages and suggest fixes
Stack Trace Decoder
Decode and explain stack traces
Memory Leak Finder
Find and fix memory leaks
Performance Profiler
Profile code and identify bottlenecks
Log Analyzer
Analyze log files and identify patterns
Network Debugger
Debug network/HTTP request issues
Race Condition Finder
Identify potential race conditions
Environment Diff
Compare environment configurations
Want a Debugging skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.