← LeaderboardThe Skill Leaderboard
rust-dashmap-guard-scope
Diagnose Rust DashMap write contention caused by a held guard. Verify same-shard keys and preserve atomic updates with the entry API.
Try your first task
A source-grounded starter task has not been verified for this skill yet.
SKILL.md
name: rust-dashmap-guard-scopedescription: Diagnose Rust DashMap write contention caused by a held guard. Verify same-shard keys and preserve atomic updates with the entry API.category: developmentrisk: criticalsource: selfsource_type: selfauthor: ShianMikedate_added: "2026-12-02"tags: [rust, dashmap, concurrency, locks, testing]Rust DashMap guard scope
When to Use
- A DashMap operation stalls while the caller still owns a reference into the same map.
- Updating a different key can require the shard held by a read or write guard.
- Shortening a guard's lifetime must preserve an atomic update to one key.
Prerequisites
- Identify the DashMap version and the exact operations producing and consuming guards.
- Trace guard ownership through temporaries, callbacks, and suspension points.
- Determine whether the operation needs an owned snapshot, one atomic key update, or a cross-key invariant.
Workflow
- Find the live guard. Locate where a value from
get,get_mut,entry, or iteration stays in scope. Check whether the caller enters the same map again or suspends while holding it. Different keys can still use the same shard. - Measure contention without blocking the fixture. Use
try_get_mutfor the write and distinguishLocked,Absent, andPresent. A locked shard is a contention result. Repeat the operation after the held guard drops. - Shorten the guarded section. Copy or clone the data needed outside the guard, then release it before another map operation, an awaited call, or a callback that can reenter the map.
- Keep single-key updates atomic. Use an
entryguard for the read and write of the same key when concurrent callers can update it. Perform the calculation and assignment inside its scope. Keep unrelated map operations and I/O outside that scope. - Verify behavior under contention. Assert that the blocked operation can proceed after release. Exercise concurrent callers of the affected update and check for lost updates. For operations spanning multiple keys, verify the synchronization that protects the full invariant.
Example
Run Release the shard guard. It deterministically chooses two different keys in one shard, observes a locked write while reading the other key, and verifies atomic increments from four workers.
Limitations
- The fixture enables DashMap's
raw-apifeature to select a same-shard pair. The guarded operations and entry update use the normal APIs. - A single
entryguard protects one key's update. A cross-key invariant needs its own synchronization policy. - The test observes contention through a nonblocking API. Check real async scheduling and throughput separately.
Safety and Side Effects
- Run the example in a scratch Cargo project. It uses synthetic in-memory values and local threads.
- Bound retry behavior when using the nonblocking APIs in an application. Handle contention according to the operation's deadline.
- Review which borrowed values escape a guard scope before changing the public return type or copying large entries.