deadlock-detection-harness
БесплатноБез исполняемых скриптовНе проверенUse when a service that takes two or more locks hangs in production with no error, or before a release when lock order across code paths was never checked…
от testland★ 18 звёзд на GitHubОб этом скилле
deadlock-detection-harness
A deadlock = a cycle in the lock-wait graph: thread A holds X waiting for Y; thread B holds Y waiting for X. Tests prevent deadlocks via static analysis of lock acquisition order + runtime detection (timeouts, sanitizer, postmortem dumps).
When to use
- Service has multiple locks/mutexes and complex code paths.
- Production hang reported; need to (a) reproduce and (b) prevent recurrence.
- Pre-release:
detect_deadlocksThreadSanitizer mode passes on the test workload.
Step 1 - Lock-order convention
Establish a strict ordering for all locks and document it:
# Lock order (acquire low-numbered first; release high-numbered first)
LOCK_ORDER = {
"user_lock": 1,
"session_lock": 2,
"audit_lock": 3,
"db_lock": 4,
}
Any code that violates this order = potential deadlock. The deadlock harness's job: detect violations.
Step 2 - Instrument lock acquisitions
For Python (rough sketch using contextvars + decorator):
import contextvars
import threading
acquired = contextvars.ContextVar("acquired", default=())
class OrderedLock:
def __init__(self, name, order):
self._lock = threading.Lock()
self.name = name
self.order = order
def __enter__(self):
held = acquired.get()
if held and held[-1].order >= self.order:
raise RuntimeError(
f"Lock-order violation: holding {[h.name for h in held]} "
f"(orders {[h.order for h in held]}), want {self.name} (order {self.order})"
)
self._lock.acquire()
acquired.set(held + (self,))
return self
def __exit__(self, *exc):
acquired.set(acquired.get()[:-1])
self._lock.release()
In tests, use OrderedLock everywhere. Violations raise
synchronously (test fails) instead of silently risking deadlock.
Step 3 - Build lock-acquire-order graph from logs
For dynamic analysis (when static order isn't enough), capture acquire events and build the graph:
from collections import defaultdict
def build_lock_graph(events):
"""events: list of (thread_id, action, lock_name)
action ∈ {'acquire', 'release'}
"""
held = defaultdict(set)
edges = defaultdict(set) # lock A → lock B = "B was acquired while A held"
for tid, action, lock in events:
if action == "acquire":
for prior in held[tid]:
edges[prior].add(lock)
held[tid].add(lock)
else:
held[tid].discard(lock)
return edges
Step 4 - Cycle detection (DFS)
def find_cycles(graph):
WHITE, GRAY, BLACK = 0, 1, 2
color = {n: WHITE for n in graph}
cycles = []
def visit(node, path):
if color.get(node) == GRAY:
cycles.append(path[path.index(node):])
return
if color.get(node) == BLACK:
return
color[node] = GRAY
for nxt in graph.get(node, ()):
visit(nxt, path + [nxt])
color[node] = BLACK
for n in list(graph):
visit(n, [n])
return cycles
def test_no_lock_cycles_in_workload():
events = run_workload_capture()
graph = build_lock_graph(events)
cycles = find_cycles(graph)
assert cycles == [], f"Lock cycles detected: {cycles}"
Any cycle = potential deadlock under the right scheduling.
Step 5 - Lock-acquire timeout (fail-fast)
def test_lock_acquired_within_timeout():
lock = threading.Lock()
holder_done = threading.Event()
def hold_forever():
with lock:
holder_done.wait() # never completes
t = threading.Thread(target=hold_forever, daemon=True)
t.start()
time.sleep(0.1)
acquired = lock.acquire(timeout=2.0)
assert acquired, "Lock not acquired within 2s - possible deadlock"
holder_done.set()
In production code, use timed acquires + escalation (log + alert) instead of indefinite wait - tests verify the contract.
Step 6 - TSan deadlock detection (C/C++/Go)
ThreadSanitizer (per the [TSan docs]) supports deadlock detection. Enable per-process:
TSAN_OPTIONS=detect_deadlocks=1:second_deadlock_stack=1 ./program
Output identifies the inconsistent lock orderings causing potential deadlocks. Catches bugs that haven't manifested as actual deadlock yet.
Step 7 - Postmortem dump analysis
When deadlock happens in production, capture state for offline analysis:
| Stack | Tool |
|---|---|
| JVM | jstack <pid> (heap-style, includes lock-graph annotation Found one Java-level deadlock) |
| Native (Linux) | gdb -p <pid> -batch -ex "thread apply all bt" |
| Go | kill -SIGQUIT <pid> (dumps all goroutine stacks + lock holders) |
| .NET | dotnet-dump collect -p <pid> + analyze with dotnet-dump analyze |
# JVM example
jstack 12345 > thread-dump.txt
grep -A 5 "Found one Java-level deadlock" thread-dump.txt
Step 8 - Lock-acquire fingerprint test
For systems where
Установить deadlock-detection-harness в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
FAQ
Что делает скилл deadlock-detection-harness?
Use when a service that takes two or more locks hangs in production with no error, or before a release when lock order across code paths was never checked. Records lock order, finds cycles, uses lock timeouts to fail instead of hang, and reads jstack or gdb thread dumps.
Как установить скилл deadlock-detection-harness?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл deadlock-detection-harness запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
mcp-builder
Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when…
от Anthropicclaude-api
Reference for the Claude API / Anthropic SDK — model ids, pricing, params, streaming, tool use, MCP, agents, caching, token counting, model migration. TRIGGER…
от Anthropicalgorithmic-art
Creating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code…
от Anthropicmcp-integration
This skill should be used when the user asks to "add MCP server", "integrate MCP", "configure MCP in plugin", "use .mcp.json", "set up Model Context…
от Anthropic