Deloxide - Cross-Language Deadlock Detector
Deloxide is a cross-language deadlock detection library with visualization support. It tracks mutex and reader-writer lock operations in multi-threaded applications to detect, report, and visualize potential deadlocks in real-time.
Table of Contents
- Features
- Building and Installation
- Quick Start
- Visualization
- Project Architecture
- Lock Order Graph
- Stress Testing
- Comparison with Other Solutions
- Performance & Validation
- Documentation
- License
Features
- Real-time deadlock detection - Detects deadlocks as they happen using a Dual Detection Architecture (WFG + LOG)
- Zero False Positives - Wait-For Graph (WFG) analysis ensures 100% precision for active deadlocks
- Optimistic Fast Path - "Always-on" monitoring with negligible overhead
- Cross-language support - Core implementation in Rust with C bindings
- Stress Testing Framework - Probabilistic scheduling with Component-Based Targeting
- Visual Diagnostics - Serverless, privacy-preserving visualization of thread interactions (see example here)
- Easy integration - Drop-in replacements for
parking_lotprimitives
[!NOTE] Cross-platform support: Rust API works on Windows, macOS, and Linux. The C API is POSIX-first and ships with pthread-based convenience macros for macOS/Linux; on Windows those macros are disabled (see below) but the core C functions are fully usable.
Building and Installation
Rust
Deloxide is available on crates.io. You can add it as a dependency in your Cargo.toml:
[]
= "1.0"
With lock order graph:
[]
= { = "1.0", = ["lock-order-graph"] }
With stress testing:
[]
= { = "1.0", = ["stress-test"] }
With logging and visualization:
[]
= { = "1.0", = ["logging-and-visualization"] }
Or install the CLI tool to showcase deadlock logs directly:
For development builds:
# Standard build
# With lock order graph feature
# With stress testing feature
# With both features
C
For C programs, you'll need to compile the Rust library and link against it:
# Build the Rust library
# With lock order graph feature
# With stress testing feature
# With both features
# Compile your C program with Deloxide
A Makefile is included in the repository to simplify building and testing with C programs. It handles building the Rust library and compiling the C test programs automatically.
C API portability notes
-
Thread ID size across FFI
- The C header uses
uintptr_tfor all thread IDs; the Rust side usesusize. This ensures correct sizes on LP64 (Linux/macOS) and LLP64 (Windows).
- The C header uses
-
pthread-based helpers are POSIX-only
- The convenience macros
DEFINE_TRACKED_THREADandCREATE_TRACKED_THREADdepend onpthread.hand are available only on non-Windows platforms. - On Windows, these macros are disabled at compile time. You can still use the full C API by manually registering thread lifecycle events.
- The convenience macros
-
Manual thread registration (Windows or custom runtimes)
- Create your thread using your platform's API.
- In the thread entry, call
deloxide_register_thread_spawn(child_tid, parent_tid)once. On the thread, get IDs fromdeloxide_get_thread_id(). - Before the thread returns, call
deloxide_register_thread_exit(current_tid).
Minimal example sketch (pseudo-C):
// In parent, capture parent thread id uintptr_t parent_tid = ; // Create thread with OS API (e.g., _beginthreadex / CreateThread) // In child thread entry: uintptr_t child_tid = ; ; // ... user work ... ;
Quick Start
Rust
Deloxide provides drop-in replacements for parking_lot synchronization primitives with added deadlock detection capabilities. These primitives are API-compatible with parking_lot and serve as near drop-in replacements for std::sync (requiring only the removal of .unwrap() calls since poisoning is not supported).
deloxide::thread
A drop-in replacement for std::thread that automatically tracks thread lifecycle events. All std::thread functions and types are available with added deadlock detection:
// All std::thread items are re-exported
pub use ;
// Custom spawn function with tracking
Using tracked threads is identical to using std::thread:
use thread;
// Spawn a tracked thread - exactly like std::thread::spawn
let handle = spawn;
// All std::thread functions work
yield_now;
sleep;
let current = current;
// Builder pattern supported
let handle = new
.name
.stack_size
.spawn
.unwrap;
// Join works the same way
let result = handle.join.unwrap;
assert_eq!;
It automatically registers thread spawn/exit events for deadlock detection, visualization, and debugging purposes.
Deloxide::Mutex
A drop-in replacement for parking_lot::Mutex. It is also a direct alternative to std::sync::Mutex, but without lock poisoning (removing the need for .unwrap() on lock acquisition):
All std::sync::Mutex methods are supported (except poisoning-related ones, as parking_lot doesn't use poisoning).
Deloxide::RwLock
A drop-in replacement for parking_lot::RwLock. It is also a direct alternative to std::sync::RwLock, but without lock poisoning:
All std::sync::RwLock methods are supported (except poisoning-related ones).
Deloxide::Condvar
A drop-in replacement for parking_lot::Condvar. It serves as a replacement for std::sync::Condvar but interacts with Deloxide::Mutex.
All std::sync::Condvar methods are supported.
Complete Usage Example
Here's a comprehensive example demonstrating all Deloxide primitives in a single scenario:
use ;
use Arc;
use Duration;
C
The C API provides a complete interface to Deloxide through include/deloxide.h. It uses opaque pointers and helper macros to simplify integration with existing C codebases.
Core C API Functions
// Initialization
int ;
int ;
void ;
int ;
// Mutex operations
void* ;
void* ;
void ;
int ;
int ;
uintptr_t ;
// RwLock operations
void* ;
void* ;
void ;
int ;
int ;
int ;
int ;
uintptr_t ;
// Condvar operations
void* ;
void* ;
void ;
int ;
int ;
int ;
int ;
// Thread tracking
int ;
int ;
uintptr_t ;
// Logging and visualization
int ;
int ;
int ;
// Stress Testing (requires "stress-test" feature)
int ;
int ;
int ;
Helper Macros
Deloxide provides convenient macros for easier usage:
// Thread tracking macros
// Define a tracked thread wrapper
// Lock with automatic tracking
// Acquire read lock
// Release read lock
// Wait on condition variable
// Signal all waiting threads
Complete C Usage Example
Here's a comprehensive example demonstrating all C API features in one program:
// Global synchronization primitives
void* counter_mutex;
void* shared_rwlock;
void* condition_mutex;
void* condition_var;
int shared_counter = 0;
int condition_ready = 0;
void
// Example 1: Mutex deadlock scenario
void*
void*
// Example 2: RwLock usage
void*
void*
// Example 3: Condvar usage
void*
void*
// Define tracked thread wrappers
int
Visualization
Deloxide includes a web-based visualization tool. After detecting a deadlock, use the showcase feature to view it in your browser:
// In Rust
showcase.expect;
// Or for the currently active log
showcase_this.expect;
// In C
;
// Or for the currently active log
;
You can also automatically launch the visualization when a deadlock is detected by calling the showcase function in your deadlock callback.
Additionally, you can manually upload a log file to visualize deadlocks through the web interface:
Project Architecture
How Deloxide Works
-
Initialization: The application initializes Deloxide. Logging and lock order checking are enabled by default if their respective features are active.
-
Resource Creation: When threads, mutexes, and reader-writer locks are created, they're registered with the deadlock detector.
-
Lock Operations: When a thread attempts to acquire a lock:
- Optimistic Fast Path: The system first attempts a fast-path acquisition using atomic operations. If successful (uncontended), it bypasses the detector entirely (zero overhead).
- Slow Path (Contended): If the lock is held, the attempt is recorded by the detector.
- A "wait-for" edge is added to the graph.
- The detector checks for cycles in the "wait-for" graph.
- If a cycle is found, a deadlock is reported.
-
Deadlock Detection: When a deadlock is detected, the callback is invoked with detailed information, including which threads are involved and which locks they're waiting for.
-
Visualization: The
showcasefunction can be called (automatically in the callback or manually) to visualize the thread-lock interactions in a web browser.
Core Components
- Dual Detection Engine
- Wait-For Graph (WFG): The default, reactive tier. Detects active circular dependencies with mathematical certainty (0 false positives).
- Lock Order Graph (LOG): An optional, proactive tier. Analyzes historical acquisition patterns to warn about potential deadlocks before they occur (requires
lock-order-graphfeature).
-
Optimistic Fast Path
- Implements an "Optimistic Fast Path" architecture using atomic release-acquire semantics.
- Bypasses the global detector entirely for uncontended locks.
- Lowers instrumentation overhead to just ~10.8ns (1.09x of standard
parking_lot), enabling "always-on" production monitoring.
-
Stress Testing Framework (Optional)
- Employs Probabilistic Concurrency Testing (PCT) to expose Heisenbugs.
- Component-Based Targeting: Intelligently injects delays into interacting lock groups.
- Achieves a 99.5% manifestation rate for latent deadlocks (vs 63.4% for passive tools).
-
Resource Tracking
- Tracks threads and locks as resources with lifecycles
- Manages parent-child relationships between threads
- Automatically cleans up resources when threads exit
-
Visualization Pipeline
- Serverless Sharing: Compresses logs into URL-safe Base64 payloads (MessagePack + Gzip).
- Privacy-First: All decoding and rendering happens client-side; no data is sent to external servers.
- Provides interactive timelines and dependency graphs.
-
Cross-Language Support
- Rust API with
Mutex,RwLock,Condvar, andthreadmodule - C API through FFI bindings in
deloxide.h - Simple macros for C to handle common operations
- Rust API with
Lock Order Graph
Deloxide includes an optional lock order graph feature that detects potential deadlocks by tracking lock acquisition ordering patterns, even when threads don't actually block. This provides early warning of dangerous lock ordering patterns that could lead to deadlocks.
Enabling Lock Order Graph
In Rust:
Enable the feature in your Cargo.toml:
[]
= { = "1.0", = ["lock-order-graph"] }
Then use the lock order checking API:
use ;
new
.with_log
// Lock order checking is enabled by default if the feature is on,
// but you can explicitly enable it (no-op) or disable it with .no_lock_order_checking()
.callback
.start
.expect;
How Lock Order Graph Works
When a thread holds lock A and then acquires lock B, the system records that A < B (A must be acquired before B). If later the system sees an attempt to acquire A while holding B (B < A), this creates a cycle in the lock order graph and indicates a potential deadlock.
Note: Lock order graph detection may report patterns that never actually deadlock (false positives). It's recommended for development and testing, not production.
Stress Testing
Deloxide includes an optional stress testing feature to increase the probability of deadlock manifestation during testing. This feature helps expose potential deadlocks by strategically delaying threads at critical points.
Enabling Stress Testing
In Rust:
Enable the feature in your Cargo.toml:
[]
= { = "1.0", = ["stress-test"] }
Then use the stress testing API:
// With random preemption strategy
new
.with_log
.with_random_stress
.callback
.start
.expect;
// Or with component-based strategy and custom configuration
use StressConfig;
new
.with_log
.with_component_stress
.with_stress_config
.start
.expect;
// Or use one of the presets
new
.with_log
.with_component_stress
.with_stress_config
.start
.expect;
In C:
Build Deloxide with the stress-test feature enabled, then:
// Enable random preemption stress testing (70% probability, 100-1000us delays)
;
// Or enable component-based stress testing
;
// Initialize detector
;
Stress Testing Modes
- Random Preemption: Randomly delays threads before lock acquisitions with configurable probability
- Component-Based: Analyzes lock acquisition patterns and intelligently targets delays to increase deadlock probability
Comparison with Other Solutions
The Rust ecosystem offers several approaches to concurrency safety, each with distinct trade-offs. Deloxide focuses on bridging the gap between "safe but slow" debugging tools and "fast but unsafe" production primitives.
The Landscape
- Static Analysis: Tools that check code at compile time. They often suffer from high false positive rates (flagging safe code as dangerous), making them noisy for complex projects.
- Passive Dynamic Detection (
parking_lot): Monitors locks asynchronously. While fast, it can miss "Heisenbugs" (transient deadlocks) because it doesn't force thread interleavings, and it reports deadlocks only after a delay (polling). - Synchronous Graph Analysis (
no_deadlocks): Checks for cycles on every lock operation. This guarantees detection but incurs prohibitive overhead (>1000x), making it unusable for real-time applications.
When to use what?
- Use
parking_lot(without detection): If pure raw performance is the absolute only metric that matters, and you have mathematically proven your system cannot deadlock (e.g., essentially lock-free designs). It avoids the ~1ns atomic overhead of Deloxide's fast path. - Use
deloxide: For everything else.- Development: Instant feedback and visualization.
- Testing: Stress testing to find Heisenbugs.
- Production: The "Optimistic Fast Path" means you get safety netting with negligible cost (and sometimes speedups in high contention).
Performance & Validation
Deloxide has been evaluated through a three-tiered testing framework: Correctness (Guaranteed Deadlocks), Stress Testing (Heisenbugs), and Performance (Micro/Macrobenchmarks).
1. Methodology
- Guaranteed Deadlock Tests: Validates that the WFG logic detects 100% of deterministic cycles (e.g., barriers enforcing circular wait).
- Heisenbug Manifestation: Uses probabilistic scheduling to force race conditions in non-deterministic scenarios (e.g., Dining Philosophers without barriers).
- False Positive Analysis: Tested against 9 complex "false positive" scenarios (e.g., Gate Guarded, Lock Order Inversion) to ensure 0% false alarm rate.
2. Microbenchmark Overhead
The "Optimistic Fast Path" ensures minimal impact on atomic operations. Deloxide incurs only ~1ns overhead per lock operation.
| Metric | STD | PL+DD | DX | DX (LOG) | DX (COMP) | ND |
|---|---|---|---|---|---|---|
| Mutex Lock | 8.7ns | 9.9ns | 10.8ns | 58.1ns | 229.4ns | 10,527ns |
| RwLock Write | 10.1ns | 12.8ns | 13.9ns | 57.7ns | 234.1ns | 10,797ns |
| RwLock Read | 13.9ns | 16.1ns | 62.4ns | 85.5ns | 222.5ns | 10,895ns |
| Condvar | 17.1µs | 17.2µs | 19.6µs | 17.4µs | 20.3µs | 2,100µs |
PL+DD = parking_lot + detection, ND = no_deadlocks. DX (COMP) intentional overhead forces thread interleaving.
3. Real-World Validation: Ray Tracing
To rigorously evaluate overhead in a realistic high-performance context, we architected a custom path-tracing renderer in Rust. Unlike data-parallel approaches (like Rayon) that isolate memory, this implementation uses a shared framebuffer architecture with a fine-grained tile-based locking strategy (16x16 pixel chunks).
- Workload: High-complexity scene with multiple material types and max recursion depth of 50 bounces.
- Contention: At 1080p resolution, workers contend for locks over 129,600 times per frame (~4kHz locking frequency).
- Result: This specifically targets the "worst-case" scenario for a deadlock detector: high-frequency, fine-grained locking.
In this saturation test, Deloxide demonstrated superior deterministic stability.
| Config | 426x240 | 854x480 | 1280x720 | 1920x1080 (Saturation) |
|---|---|---|---|---|
| STD | 0.81s ± 0.03 | 3.41s ± 0.15 | 7.33s ± 0.31 | 17.22s ± 0.65 |
| PL+DD | 0.81s ± 0.00 | 3.26s ± 0.02 | 7.19s ± 0.03 | 18.32s ± 0.06 |
| DX (Default) | 0.80s ± 0.00 | 3.18s ± 0.01 | 7.09s ± 0.03 | 16.67s ± 0.09 |
| ND | 33.0s ± 31.4 | 220.9s ± 182 | 192.9s ± 281 | 329.1s ± 554 |
Key Result: At 1080p, Deloxide (16.67s) is 9% faster than the baseline parking_lot (18.32s) and significantly more stable (CV < 0.6%) than STD.
Clarification: Deloxide outperforms the baseline not because detection makes it faster, but because our underlying mutex implementation handles high-contention tail latency better than the OS primitive.
4. Heisenbug Manifestation Rates (Stress Testing)
Comparison of manifestation rates (1000 iterations) across different strategies. Active stress testing is required to find latent bugs.
| Scenario | DX (Passive) | DX (RAND) | DX (AGG) | DX (COMP) | PL+DD | ND |
|---|---|---|---|---|---|---|
dining_philosophers |
40.3% | 65.4% | 83.6% | 98.7% | 54.1% | 75.5% |
five_lock_cycle |
100.0% | 100.0% | 100.0% | 100.0% | 100.0% | 99.4% |
rwlock_deadlock |
41.5% | 71.1% | 88.1% | 100.0% | 60.4% | 100.0% |
three_lock_cycle |
88.7% | 98.7% | 99.4% | 100.0% | 77.4% | 98.7% |
two_lock |
17.6% | 57.2% | 81.8% | 98.7% | 25.2% | 73.6% |
| Average Rate | 57.6% | 78.5% | 90.6% | 99.5% | 63.4% | 89.4% |
5. False Positive Analysis
Deloxide (WFG) achieved 0 False Positives, utilizing a mathematically rigorous cycle detection algorithm.
- Tested Patterns: Gate Guarded, Lock-Free Intervals, Lock Order Inversion, Thread-Local Hierarchies.
- Result: Perfect specificity.
Feature Matrix
| Feature | STD | PL+DD | ND | DX |
|---|---|---|---|---|
| Mutex Overhead | 0.88x | 1.00x | 1063.33x | 1.09x |
| Ray Tracing (1080p) | 0.94x | 1.00x | 17.96x | 0.91x (Faster) |
| Detection Method | None | Async (Poll) | Synchronous | Synchronous (Instant) |
| Lock Order Analysis | No | No | No | Yes |
| Stress Testing | No | No | No | Yes |
| Visualization | No | No | Text Dump | Interactive URL |
| False Positive Rate | N/A | Zero | Zero | Zero (WFG) |
STD = std::sync, PL+DD = parking_lot with deadlock_detection, ND = no_deadlocks, DX = Deloxide
Documentation
For more detailed documentation:
- Crates.io:
https://crates.io/crates/deloxide - Rust Docs:
https://docs.rs/deloxide - C API: See
include/deloxide.handhttps://docs.rs/deloxide/latest/deloxide/ffi/index.html
License
Deloxide is licensed under the terms of the MIT license, the Apache License (Version 2.0), or the Coffeeware License, at your option.
Option 1: The "Serious" Licenses
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Option 2: The "Fun" License
/*
* ( (
* ) )
* ........
* | |] ☕
* \ /
* `----'
*
* "THE COFFEEWARE LICENSE" (Revision 1, Deloxide Edition):
* (Inspired by the original Beerware License by Poul-Henning Kamp)
*
* Emirhan Tala and Ulaş Can Demirbağ wrote this file. As long as you retain
* this notice, you can do whatever you want with this stuff — run it, fork it,
* deploy it, tattoo it, or summon it in a thread ritual. We don't care.
*
* Just remember: we make no guarantees, provide no warranties, and accept no
* responsibility for anything that happens. This software may or may not work,
* may or may not cause your system to spontaneously combust into deadlocks,
* and may or may not summon a sentient debugger from the void. But we accept
* coffee! If we ever meet someday and you think this code helped you can buy
* us a coffee in return. Or not. No pressure. But coffee is nice. We love it!
* ----------------------------------------------------------------------------
*/