agentleFS
gurinderugurinderu/craft/skills/rust-concurrency/SKILL.md

rust-concurrency

Skill1 starsChanged 41 days agoMITView on GitHub

Rust concurrency and async — threads vs async, Send/Sync, sharing with Arc/Mutex/RwLock, channels, tokio tasks, holding locks across await, and deadlocks. Use when parallelizing work, sharing state across threads, choosing threads vs async, or fixing Send/Sync errors and deadlocks. Triggers: tokio, Send/Sync, mpsc, deadlock, future is not Send, rayon, parking_lot.

10 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use rust-api-design.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • adxptivedSkill

    rust-api-design

    Rust API Design guidelines, naming conventions, and best practices. Use when: - User is designing a library, crate, or public APIs - Implementing patterns like Builder, Typestate, or Extension traits - Designing public structures, enums, or traits - Working with common traits (From/Into, Default, AsRef, Debug, Clone) - Applying #[must_use] or #[non_exhaustive] attributes - Restricting trait implementation via Sealed Traits - Formatting naming structures and lifetime designs

    Also in 3 projects that use this

The file

We split this skill into the rules it gives an agent

2 rules. 0 are common, 2 are distinctive, and 0 are very specific to this project.

  • 0

    Common

    Most of the 9 edited versions we read keep these.

  • 2

    Distinctive

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Don't reach for async by default.

    line 28
  2. Distinctive:

    Prefer message passing. "Share memory by communicating" — channels turn data races into ownership transfers the compiler already checks.

    line 41

“Common” is measured: kept by at least 6 in 10 of the 9 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Rust Concurrency

Two questions decide everything: what kind of work (CPU vs I/O) and what's the sharing model (messages vs shared memory). Get those right and the Send/Sync errors resolve themselves. Mechanics in the sub-files.

When to Use

  • Parallelizing work across threads, or running many I/O tasks concurrently
  • Sharing state across threads (Arc, Mutex, RwLock, channels)
  • Deciding between OS threads and async
  • Fixing "cannot be sent between threads" / "future is not Send" / deadlocks

Decision 1 — threads or async?

WorkloadUseWhy
CPU-bound (compute, parsing, hashing)OS threads — std::thread, or rayon for data parallelismreal parallelism across cores; async gives you nothing here
I/O-bound (network, many sockets, DB)async — tokiothousands of cheap tasks, no thread-per-connection
Mixedasync runtime + spawn_blocking for the CPU/blocking partskeep the executor unblocked

Don't reach for async by default. If you have a handful of CPU tasks, threads are simpler and faster. Async earns its complexity at high I/O concurrency. Details: threads.md, async.md.

Decision 2 — how do threads share?

SharingUse
Nothing — hand off ownershipchannels (message passing) — the default, fewest footguns
Read-only shared dataArc<T>
Shared mutable dataArc<Mutex<T>> (or Arc<RwLock<T>> for read-heavy)
Simple counters/flagsatomics (AtomicUsize, AtomicBool)

Prefer message passing. "Share memory by communicating" — channels turn data races into ownership transfers the compiler already checks.

Higher-level compositions — structured concurrency (JoinSet), cooperative cancellation (CancellationToken), the actor pattern (own state in one task), graceful shutdown of a task tree, and choosing watch/broadcast/DashMap — are in patterns.md.

Send and Sync

Two auto-traits the compiler derives; they're the whole basis of thread safety.

  • Send — a value can be moved to another thread. Almost everything is; Rc and raw pointers are not.
  • Sync — &T can be shared across threads (equivalently: T: Sync ⇔ &T: Send). Mutex/RwLock are Sync; RefCell/Cell are not.

The single-thread vs multi-thread pointer pairs:

Single thread (rust-ownership)Across threads (here)
Rc<T> (not Send/Sync)Arc<T> (atomic count)
RefCell<T> (not Sync)Mutex<T> / RwLock<T>

Error → fix

ErrorCauseFix
E0277 Rc cannot be sent between threadsRc/RefCell shared across threadsRc→Arc, RefCell→Mutex; or pass via a channel
E0277 future is not Senda non-Send value (e.g. Rc, MutexGuard) is held across an .awaitdrop it before the await, or don't use a non-Send type (see async.md)
closure may outlive borrowed value (spawn)thread closure borrows a localmove the data in, or use std::thread::scope (see threads.md)
deadlock (no error — it hangs)locks acquired in inconsistent order, or a lock held across .awaitconsistent lock order; never hold a lock across .await

Defects vs failures here

A poisoned Mutex (lock() returns Err because a holder panicked) is a defect, not a domain failure — see the failures-vs-defects model in rust-errors. Don't model PoisonError as a recoverable case; unwrap()/expect() the lock or propagate it as opaque.

Boundary

  • Single-threaded sharing/mutation (Rc, RefCell, Cell) → rust-ownership.
  • Timeouts and retry around concurrent I/O → rust-errors (recovery).

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.