Deadlock with CountDownLatch

131 views
Skip to first unread message

Alex Kahn

unread,
Feb 1, 2016, 4:29:47 PM2/1/16
to Concurrent Ruby
Hi,

I'm on Ruby 2.1.1 with Concurrent Ruby 1.0.0. When I run the code `CountDownLatch.new.wait` I get the following error:

irb(main):001:0> require 'concurrent'
=> true
irb(main):002:0> Concurrent::CountDownLatch.new.wait
fatal: No live threads left. Deadlock?
        from /Users/akahn/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/concurrent-ruby-1.0.0/lib/concurrent/synchronization/mri_lockable_object.rb:43:in `sleep'
        from /Users/akahn/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/concurrent-ruby-1.0.0/lib/concurrent/synchronization/mri_lockable_object.rb:43:in `wait'
        from /Users/akahn/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/concurrent-ruby-1.0.0/lib/concurrent/synchronization/mri_lockable_object.rb:43:in `ns_wait'
        from /Users/akahn/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/concurrent-ruby-1.0.0/lib/concurrent/synchronization/abstract_lockable_object.rb:43:in `ns_wait_until'
        from /Users/akahn/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/concurrent-ruby-1.0.0/lib/concurrent/atomic/mutex_count_down_latch.rb:21:in `block in wait'
        from /Users/akahn/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/concurrent-ruby-1.0.0/lib/concurrent/synchronization/mri_lockable_object.rb:38:in `block in synchronize'
        from /Users/akahn/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/concurrent-ruby-1.0.0/lib/concurrent/synchronization/mri_lockable_object.rb:38:in `synchronize'
        from /Users/akahn/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/concurrent-ruby-1.0.0/lib/concurrent/synchronization/mri_lockable_object.rb:38:in `synchronize'
        from /Users/akahn/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/concurrent-ruby-1.0.0/lib/concurrent/atomic/mutex_count_down_latch.rb:21:in `wait'
        from (irb):2
        from /Users/akahn/.rbenv/versions/2.1.1/bin/irb:11:in `<main>'

Am I doing something wrong or is this a bug? I also tried with concurrent-ruby-ext but got the same result.

Thanks,
Alex 

Jerry D'Antonio

unread,
Feb 1, 2016, 4:56:43 PM2/1/16
to Alex Kahn, Concurrent Ruby
Alex,

You are using the latch wrong.

A CountDownLatch is a synchronization object shared between threads. One thread waits on the latch while another thread counts down. The `#wait` method block until the latch count reaches zero. Since you only have one thread (the main thread) you are hopelessly blocking the main thread forever. When Ruby detects the deadlock it raises an exception. This is the expected behavior for both CountDownLatch and Ruby.

Many good examples of how CountDownLatch is used can be found within the concurrent-ruby test suite. It's also used a few places in Rails 5 and in the Sucker Punch gem (both the library and its tests). One good example is here: https://github.com/ruby-concurrency/concurrent-ruby/blob/master/spec/concurrent/atomic/count_down_latch_spec.rb#L59

Best regards,
Jerry


--
You received this message because you are subscribed to the Google Groups "Concurrent Ruby" group.
To unsubscribe from this group and stop receiving emails from it, send an email to concurrent-ru...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alex Kahn

unread,
Feb 1, 2016, 5:02:22 PM2/1/16
to Jerry D'Antonio, Concurrent Ruby
Thanks, Jerry. I was indeed trying to block the main thread indefinitely. I didn't realize that Ruby would detect this as a deadlock and raise an error!

Cheers,
Alex


Jerry D'Antonio

unread,
Feb 2, 2016, 8:48:38 AM2/2/16
to Alex Kahn, Concurrent Ruby
Why are you blocking the main thread when there are no other threads running? MRI will only raise the deadlock error if there are no other threads. If there are other threads running then you can block the main thread indefinitely without error (until the last other thread dies, that is). But a blocked main thread without other threads is basically a dead program so I'm confused as to what you are trying to accomplish. Can you describe what you are trying to do in your program?

PS: I post that response in Gitter, too.

Best regards,
Jerry

Reply all
Reply to author
Forward
0 new messages