Skip to content

Conversation

@fzakaria
Copy link
Contributor

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 #796

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
@fzakaria fzakaria requested a review from pitr-ch February 10, 2020 19:19

class CRubySet < ::Set
def initialize(*args)
self.send(:_mon_initialize)
Copy link
Contributor Author

@fzakaria fzakaria Feb 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this -- certain Sets are being created not through initialize
I would see:

when initializing with a block argument
concurrent-ruby/concurrent/thread_safe/util/data_structures.rb:43: 
warning: instance variable @_monitor not initialized

I'm not certain how the Set is being initialized without going through:

 def self.new(*args)
    obj = super(*args)
    obj.send(:_mon_initialize)
    obj
  end

TBH i'm

expect(set).to be_empty
end

it 'force context switch' do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

writing a test for this is actually not easy lol.

What this ended up testing was that during a method of set it grabs a monitor and locks out any other changes.

expect(set.add?(1)).to eq set

the above line should occasionally return nil if the methods were not synchronized.

@pitr-ch
Copy link
Member

pitr-ch commented Jun 4, 2021

Replaced by #911

@pitr-ch pitr-ch closed this Jun 4, 2021
@eregon eregon mentioned this pull request Nov 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Concurrent::Set is not thread-safe in CRuby

2 participants