Deadlock in Concurrent::Array's on non-MRI

Open
#627 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
ruby
Domain
backend

Research direction

Start with the linked gist and the Concurrent::Array documentation, then reproduce the behavior on MRI, JRuby, Rubinius, and TruffleRuby. Compare block-taking methods on Concurrent::Array and Concurrent::Hash; done requires a maintainer decision on intended locking semantics and whether behavior should be consistent across implementations.

Written by the indexing model from the issue text.

Description

bug looking-for-contributor medium-priority
  • concurrent-ruby version: 1.0.4
  • concurrent-ruby-ext installed: no
  • concurrent-ruby-edge used: no

Take the following code: https://gist.github.com/eregon/323890bfff539f8a33f66d8b2f02cc99
Of course, its purpose is to create a deadlock but it means any method taking a block on Array can cause a deadlock as long as:

  • 2+ threads use 2+ Concurrent::Array by calling a method taking a block
  • inside the blocks, the threads call any method taking on another Concurrent::Array

This seems not such a rare scenario, as it is frequent to call methods in a block that do not only involve the current Array.

The deadlock does not happen on MRI, as it uses a single global lock and concurrent-ruby just subclasses ::Array. It does happen on all other implementations though, like JRuby, Rubinius and TruffleRuby.

The documentation says:

A thread-safe subclass of Array.
This version locks against the object itself for every method call,
ensuring only one thread can be reading or writing at a time.
This includes iteration methods like #each.

So indeed this might imply the deadlock above, but it's not clear.
ensuring only one thread can be reading or writing at a time is also inaccurate on MRI for methods taking a block, as they release the GIL and might switch to another Thread in the middle of e.g. #each.

The same apply for Hash.

Is this behavior intended? Should this be fixed?
Should it behave the same on the different implementations?

Dominant language
Ruby
Stars
5.8k
Forks
420
Avg merge
20h 45m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ruby-concurrency/concurrent-ruby

All issues in ruby-concurrency/concurrent-ruby

Similar issues

More Ruby issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.