Closed mensfeld closed 1 year ago
Yeah, that definitely seems like a bug. Could you please submit a PR to fix this up?
@radar there are two ways to fix this and want to make sure I pick one that is more suitable:
Concurrent::Hash
with Concurrent::Map
including checks that their API coverage is the same for i18n.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.