Skip to content
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

#get is not thread safe #1068

Closed
ioquatix opened this issue Oct 26, 2019 · 4 comments
Closed

#get is not thread safe #1068

ioquatix opened this issue Oct 26, 2019 · 4 comments
Labels

Comments

@ioquatix
Copy link
Contributor

def get(name)
klass = @constants[name]
return klass if klass
Object.const_get(name).tap { |c| set(c, name) }
end
def set(klass, name = nil)
name ||= klass.to_s
@lock.synchronize do
@constants[name] = klass
end
end

Reading from a hash table while there are updates happening is not thread safe.

@iMacTia
Copy link
Member

iMacTia commented Oct 30, 2019

Ah I see what you mean, should we move the @lock.synchronise call into get instead?
I'm not sure we'd need it in set anymore after that?

@ioquatix
Copy link
Contributor Author

You need it in both methods because you are reading and writing to @constants. You can't have synchronised access to the same hash table in any sitautions. Read only operations are not safe with simultaneous writes.

@olleolleolle
Copy link
Member

Alright, I merged a change to wrap the @constants in a synchronize.

@olleolleolle
Copy link
Member

Thanks, @ioquatix for the inspiration!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants