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.
Features
- Real-time deadlock detection - Detects deadlocks as they happen
- Cross-language support - Core implementation in Rust with C bindings
- Thread & lock tracking - Monitors relationships between threads and sync primitives (Mutex, RwLock, Condvar)
- Visualization - Web-based visualization of thread-lock relationships
- Low overhead - Designed to be lightweight for use in production systems
- Easy integration - Simple API for both Rust and C
- Stress testing - Optional feature to increase deadlock manifestation during testing
[!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.
Project Architecture
How Deloxide Works
-
Initialization: The application initializes Deloxide with optional logging and callback settings.
-
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:
- The attempt is recorded by the detector
- If the lock is already held by another thread, a "wait-for" edge is added
- 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
-
Deadlock Detection Engine
- Maintains a "wait-for" graph of thread dependencies
- Detects cycles in the graph to identify potential deadlocks
- Reports detected deadlocks through a configurable callback
-
Resource Tracking
- Tracks threads and locks as resources with lifecycles
- Manages parent-child relationships between threads
- Automatically cleans up resources when threads exit
-
Logging and Visualization
- Records thread-lock interactions to a log file
- Processes logs for visualization in a web browser
- Provides automatic visualization when deadlocks are detected
-
Cross-Language Support
- Rust API with
Mutex,RwLock, andThreadtypes - C API through FFI bindings in
deloxide.h - Simple macros for C to handle common operations
- Rust API with
-
Stress Testing (Optional with stress-testing feature)
- Strategically delays threads to increase deadlock probability
- Multiple strategies for different testing scenarios
- Available as an opt-in feature for testing environments
Quick Start
Rust
Deloxide provides drop-in replacements for standard synchronization primitives with deadlock detection capabilities. All primitives wrap parking_lot implementations and add unique identifiers for tracking and visualization.
Deloxide::Thread
A wrapper around std::thread::JoinHandle that automatically tracks thread lifecycle events:
;
Creating tracked threads is identical to standard threads:
use Thread;
// Spawn a tracked thread - just like std::thread::spawn
let handle = spawn;
// Join works the same way
let result = handle.join.unwrap;
assert_eq!;
It additionally generates a thread ID for deadlock detection, visualization and debugging purposes.
Deloxide::Mutex
A wrapper around parking_lot::Mutex that tracks lock operations:
Deloxide::RwLock
A wrapper around parking_lot::RwLock supporting both read and write operations:
Deloxide::Condvar
A wrapper around parking_lot::Condvar for condition-based synchronization:
Complete Usage Example
Here's a comprehensive example demonstrating all Deloxide primitives in a single scenario:
use ;
use Arc;
use Duration;
use thread;
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 and cleanup
int ;
void ;
// Mutex operations
void* ;
void ;
void ;
void ;
int ;
// RwLock operations
void* ;
void ;
void ;
void ;
void ;
void ;
// Condvar operations
void* ;
void ;
void ;
void ;
void ;
// Thread tracking
void ;
void ;
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
C API Portability Notes
- Linux/macOS: Full pthread support, all features available
- Windows: Requires pthread-compatible library. Refer to [C API portability notes]
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:
[]
= { = "0.2.1", = ["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;
In C:
Build Deloxide with the stress-test feature enabled, then:
// Enable random preemption stress testing (70% probability, 1-10ms 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
[!NOTE] Condvar wake-ups (notify_one/notify_all) trigger a synthesized mutex attempt for the woken thread to model the required mutex re-acquisition. Stress injection occurs on this synthetic mutex attempt (and on normal lock attempts), not directly on the condvar wait/notify operations.
Building and Installation
Rust
Deloxide is available on crates.io. You can add it as a dependency in your Cargo.toml:
[]
= "0.2.1"
With stress testing:
[]
= { = "0.2.1", = ["stress-test"] }
Or install the CLI tool to showcase deadlock logs directly:
For development builds:
# Standard build
# With stress testing feature
C
For C programs, you'll need to compile the Rust library and link against it:
# Build the Rust library
# With stress testing feature
# 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 ... ;
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:
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
Performance & Evaluation
This part outlines the performance, deadlock detection capabilities, and robustness of Deloxide. We compare it against standard Rust mutexes (std::sync::Mutex), parking_lot::Mutex (with its deadlock_detection feature), and the no_deadlocks library.
Key Takeaways (TL;DR):
- Performance:
Deloxideintroduces a manageable performance overhead in many common scenarios but can be more significant under heavy lock contention. - Deadlock Detection:
Deloxide's optional stress testing modes are exceptionally effective at uncovering hard-to-find "Heisenbug" deadlocks that are often missed by other detectors. - Superior Speed:
Deloxidedetects deadlocks up to 80x faster than competing libraries, providing an immediate feedback loop for developers. - Reliability:
Deloxideis robust and does not produce false alarms in deadlock-free code.
All benchmarks were run on a base M1 MacBook Pro with Rust 1.86.0-nightly.
[!IMPORTANT] The following benchmarks were conducted using version v0.1.0 and currently cover only Mutex performance. Benchmarks for RwLock and Condvar will be added in future updates.
1. Performance Overhead
We evaluated overhead using both low-level microbenchmarks and application-level macrobenchmarks.
Microbenchmark Overhead
These tests measure the raw performance of creating a mutex and performing a single, uncontended lock/unlock cycle.
| Tested Setup | Mutex Generation Time (ns) | Lock/Unlock Time |
|---|---|---|
| Std | 17.4 ± 0.16 ns | 8.5 ± 0.07 ns |
| ParkingLot | 16.4 ± 0.27 ns | 9.7 ± 0.07 ns |
| NoDeadlocks | 31.6 ± 0.20 ns | 10.6 ± 0.11 µs |
| Deloxide (Default) | 36.2 ± 0.28 ns | 82.1 ± 0.38 ns |
Deloxide+StressRand |
36.4 ± 0.23 ns | 3.2 ± 1.06 ms |
Deloxide+StressComp |
36.3 ± 0.27 ns | 241.6 ± 4.08 ns |
(Lower is better)
Deloxide's mutex creation and lock/unlock operations carry a higher base cost than std or parking_lot due to the integrated, real-time detection logic that runs on every operation.
Application-Level Overhead
We simulated two common application workloads to measure performance at scale.
A) Hierarchical Locking Benchmark
This benchmark involves multiple threads acquiring a sequence of locks, simulating scenarios with complex, multi-lock dependencies.

Analysis:
- In this scenario,
Deloxide's baseline overhead is modest. At the 32x32 scale, it is ~1.62x slower thanstd::sync::Mutex(526.0µs vs 324.2µs). - The stress testing modes (
Deloxide+StressRand,Deloxide+StressComp) perform as expected, trading performance for improved bug detection, hence their significantly higher runtimes. - The
NoDeadlockslibrary showed very high execution times and was not run at larger scales.
B) Producer-Consumer Benchmark
This benchmark models a high-contention scenario where multiple producer and consumer threads access a single shared queue protected by a mutex.

Analysis:
- Under heavy contention for a single lock,
Deloxide's overhead is more pronounced. At the 4x4 scale, it is ~5.4x slower thanstd(1.7ms vs 309.4µs). - The performance of
Deloxide+StressRand(28.0s) andNoDeadlocks(7.1s) at the 4x4 scale made testing at larger scales impractical. - This benchmark highlights that
Deloxide's overhead is most noticeable in applications with a central, highly-contended bottleneck.
2. Deadlock Detection Capability
The primary goal of Deloxide is to find deadlocks. We tested its ability to detect "Heisenbugs"—elusive deadlocks that only occur under specific, rare thread interleavings. A superior detector not only finds these bugs but does so quickly, providing rapid feedback to the developer.
The table below shows the percentage of runs (out of 1000) where a deadlock was successfully detected, alongside the average time it took to find it.
| Tested Setup | Two-Lock Scenario | Two-Lock Scenario | Three-Lock-Cycle Scenario | Three-Lock-Cycle Scenario |
|---|---|---|---|---|
| Detection Rate | Mean Time (ms) | Detection Rate | Mean Time (ms) | |
| Deloxide (Default) | 5.9% | 2.7 | 0.2% | 45.9 |
Deloxide+StressRand |
51.2% | 48.8 | 66.9% | 158.5 |
Deloxide+StressAggrRand |
57.0% | 56.4 | 75.3% | 124.4 |
Deloxide+StressComp |
4.6% | 15.0 | 100.0% | 16.8 |
| ParkingLot | 3.7% | 4.9 | 2.9% | 5.8 |
| NoDeadlocks | 100.0% | 1127.0 | 98.9% | 1370.1 |
(Lower time is better)
Analysis:
- Without stress testing,
Deloxide's detection rate for these rare deadlocks is low, similar toparking_lot. This is expected, as the deadlock condition rarely manifests naturally. - Stress testing is the killer feature. Enabling random preemption (
StressRand) dramatically increases the detection rate to over 50-75%, while the component-based strategy (StressComp) achieved a perfect 100% detection rate for the complex three-lock cycle. - Superior Detection Speed: The most critical finding is the time to detection.
Deloxide+StressCompfound the three-lock deadlock in just 16.8 ms.- In contrast,
NoDeadlockstook 1,370 ms (1.4 seconds) to detect the same bug.
3. False Positive Analysis
A deadlock detector must be reliable. We verified that Deloxide does not report deadlocks in correctly written, deadlock-free code.
We ran two deadlock-free scenarios 100 times each:
- Gate Guarded: Threads lock A then B, or B then A, but use a gate to prevent circular waits.
- Four Hierarchical: Locks are always acquired in a globally consistent order (A → B → C → D).
Result:
Across all tests, Deloxide (in all configurations), parking_lot, and no_deadlocks all passed with zero false positives.
See tests/examples in /tests or /c_tests
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!
* ----------------------------------------------------------------------------
*/