ruby-concurrency

Advises on Ruby threads, fibers, Ractors, synchronization primitives, and the concurrent-ruby gem.

5|Updated Sep 24, 2017
One-click install
npx skills add https://github.com/madyankin/dotfiles --skill ruby-concurrency-madyankin
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: ruby-concurrency
Source: https://github.com/madyankin/dotfiles/tree/main/.config/agents/skills/ruby-concurrency
Command: npx skills add https://github.com/madyankin/dotfiles --skill ruby-concurrency-madyankin

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Choosing and correctly implementing concurrency in Ruby is hard: the GVL limits thread parallelism, Ractors impose strict object isolation, and synchronization bugs like race conditions and deadlocks are easy to introduce. This Skill provides expert guidance on picking the right concurrency primitive and implementing it correctly. ## Core Features & Use Cases - Concurrency Model Guidance: Explains GVL mechanics, M:N scheduling, and when threads, fibers, Ractors, or processes actually run in parallel. - Synchronization Patterns: Covers Mutex, ConditionVariable, Monitor, SizedQueue, Semaphore, and atomic types with deadlock-prevention rules. - concurrent-ruby & async: Documents Promises, thread pools, Concurrent::Map, Atom, and the Fiber Scheduler interface for non-blocking I/O. - Use Case: When parallelizing HTTP requests in a Rails background job, use this Skill to decide between threads and the async gem, then implement a worker pool with proper exception handling and graceful shutdown. ## Quick Start Ask how to safely parallelize an I/O-bound or CPU-bound Ruby task, for example how to fetch multiple URLs concurrently or share state between threads without race conditions.

Frequently Asked Questions about ruby-concurrency

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I run parallel HTTP requests in Ruby?▼

Use threads with Net::HTTP since the GVL is released during blocking I/O, or use the async gem with Async::HTTP::Internet for fiber-based concurrency in a single thread. Threads are simpler; async scales to far more concurrent requests.

Threads vs Ractors in Ruby: which should I use?▼

Use threads for I/O-bound work since the GVL is released during blocking calls. Use Ractors for CPU-bound pure-Ruby work, since each Ractor has its own GVL and runs in true parallel, but only shareable (frozen) objects can cross Ractor boundaries.

Does the GVL make Ruby threads safe?▼

No. The GVL serializes bytecode execution but does not guarantee thread safety. Non-atomic operations like n += 1 or hash[k] ||= [] can interleave across GVL handoffs, so shared mutable state still requires a Mutex or atomic types.

Why does my Ruby code deadlock with Mutex?▼

Deadlocks occur when threads acquire multiple mutexes in different orders, or when a thread re-locks a non-reentrant Mutex it already holds. Acquire locks in a consistent global order, keep critical sections short, or use Monitor for re-entrant locking.

Can I use database connections inside Ractors?▼

No. Most C extensions including pg and mysql2 are not Ractor-safe, and connection pools cannot be shared across Ractors. Each Ractor would need its own connection, making threads or processes a better fit for database-heavy workloads.

When should I use fibers instead of threads in Ruby?▼

Use fibers for cooperative patterns like generators, lazy sequences, and state machines, or with a Fiber Scheduler (via the async gem) for high-concurrency I/O. Fibers cost ~4KB versus ~1MB per thread and never preempt, but provide no parallelism.