New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Concurrent::Set is not thread-safe in CRuby #796
Comments
I have revised the example to have the context switch happen in the middle of the execution of |
Hi @fxn, you are right, the comment should be dropped (it is incorrect). concurrent-ruby/lib/concurrent/set.rb Lines 26 to 27 in b20a562
Furthermore, currently it looks like Set is not safe to use beyond basic |
@fxn thanks for filing the issue! |
I'd like to work on this. What would the fix entail though? Changing the implementation in mri ruby or providing a thread safe interface here? |
@anuragshopify thank you! I've assigned the issue to you. I would recommend to inherit from the MRI's Set in concurrent-ruby and fixing the methods in the new class here. Then you could try to contribute it back to MRI. |
Follow similar pattern for Rbx to wrap every superclass (::Set) instance method with a Monitor (which is re-entrant). Included is a test that I believe *would* occasionally fail if Set was not thread safe. fixes ruby-concurrency#796
@fxn is my stab at the fix: https://github.com/ruby-concurrency/concurrent-ruby/pull/847/files |
@fxn thank you for the PR! I have a proper look in few days. I've run out of time today. |
🎉 |
Concurrent::Set
returnsSet
in CRuby with this argument:As discussed in #793, that is not enough for thread-safety, you also need the interface to be atomic somehow. For
Hash
andArray
this holds because they are written in C, butSet
is partially written in Ruby.To demonstrate
Concurrent::Set
is not thread-safe I have created this artificial example:By the time
delete?
is invoked, the set has the element 1, therefore, that method should returnself
(the set). However, it returnsnil
becauseclear
was called during a context switch in the middle of the method execution.The text was updated successfully, but these errors were encountered: