ruby-concurrency / ruby-concurrency/concurrent-ruby

Concurrent::Hash and Concurrent::Array are not fully threadsafe on CRuby

Open
#929 9 comments 1 reaction 1 assignee View on GitHub

@eregon is already working on this.

Since Mar 10, 2023.

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

Description

Concurrent::Hash and Concurrent::Array are not fully threadsafe on CRuby.

This can be demonstrated for Array with:

require 'concurrent/array'

array = Concurrent::Array.new
array << 0

20.times.map do |i|
  Thread.new { array.map!{|v| sleep 0.001; v + 1} }
end.each(&:join)
p array

This returns [1].

For Hash this code shows the issue:

require 'concurrent/hash'

hash = Concurrent::Hash.new
hash[:a] = 0

20.times.map do |i|
  Thread.new { hash.transform_values!{|v| sleep 0.001; v + 1} }
end.each(&:join)
p hash

This returns {a: 1}.

In both cases we would expect to see 20 instead of 1.

* Ruby implementation:             Ruby
* `concurrent-ruby` version:       1.1.9
* `concurrent-ruby-ext` installed: no
* `concurrent-ruby-edge` used:     no

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.