Ruby: "threaderror: deadlock; recursive locking" error

Example:

mutex = Mutex.new
mutex.lock
mutex.lock
Occurs when a thread tries to lock a mutex that it has already locked.

Solution:

# Avoid recursively locking the same mutex or use Mutex#synchronize.
mutex.synchronize do
  # Critical section code here
end

Beginner's Guide to Ruby