You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# run after initial count 1100.times{Thread.new{I18n.backend.store_translations(rand.to_s,:foo=>{:bar=>'bar',:baz=>'baz'})}}I18n.available_locales.count#=> 101
What I expected to happen
I expected all the languages to be loaded and not one.
is not thread-safe. The hash initialization for a given language may not be set correctly because it will be evaluated several times.
Versions of i18n, rails, and anything else you think is necessary
i18n taken from master.
Other info
Happy to fix this with a PR. This may seem like a weird edge case though the side effects may also be weird: some translations may not be loaded as expected while others may work and it may be close to impossible to figure out the reason.
The text was updated successfully, but these errors were encountered:
@radar there are two ways to fix this and want to make sure I pick one that is more suitable:
Replace Concurrent::Hash with Concurrent::Map including checks that their API coverage is the same for i18n.
Wrap the initialization operation with a Mutex.
Any of this will work. I would prefer 2 because it brings less risk to the table and mutex will apply only for initial loading, not for the cached state.
What I tried to do
Try to load several languages in different threads:
Check how many languages are loaded:
Consecutive executions give correct result:
What I expected to happen
I expected all the languages to be loaded and not one.
What actually happened
This: https://github.com/ruby-i18n/i18n/blob/master/lib/i18n/backend/simple.rb#L71
is not thread-safe. The hash initialization for a given language may not be set correctly because it will be evaluated several times.
Versions of i18n, rails, and anything else you think is necessary
i18n taken from master.
Other info
Happy to fix this with a PR. This may seem like a weird edge case though the side effects may also be weird: some translations may not be loaded as expected while others may work and it may be close to impossible to figure out the reason.
The text was updated successfully, but these errors were encountered: