Skip to content

Make translations loading thread-safe#644

Merged
radar merged 2 commits intoruby-i18n:masterfrom
mensfeld:master
Dec 5, 2022
Merged

Make translations loading thread-safe#644
radar merged 2 commits intoruby-i18n:masterfrom
mensfeld:master

Conversation

@mensfeld
Copy link
Contributor

close #643

@radar radar merged commit 75fc49b into ruby-i18n:master Dec 5, 2022
@chaadow
Copy link
Contributor

chaadow commented Dec 28, 2025

hi @mensfeld I had an issue with loading not being thread safe:
image

basically since eager load is disabled, two sidekiq threads are trying to load I18n at the same time resulting into this

I'm going to eager load at bootup, but was wondering if I can just do

MUTEX.synchronize do
  Utils.deep_merge!(translations[locale], data)
end

or maybe add the synchronize here:

data.each { |locale, d| store_translations(locale, d || {}, skip_symbolize_keys: keys_symbolized) }

or wrap the entire store_translations into a synchronize.

what do you think is the best way please 🙏🏼 ( i wanna ask a real person, i dont wanna ask an AI. ty so much)

chaadow added a commit to chaadow/i18n that referenced this pull request Dec 28, 2025
ruby-i18n#644 (comment)

When two threads lazy load the translations at the same time, there is a race condition while iterating through the `translations` hash. The first thread merges new values into the hash, and the second thread, while iterating, gets `can't add a new key into hash during iteration`

The solution suggested is to use Mutex while merging the hash (which is a mutating action since we're using `merge!` instead of `merge` ) to prevent this from happening

cc @mensfeld
@chaadow
Copy link
Contributor

chaadow commented Dec 28, 2025

I went ahead and created this PR: #729

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.

[BUG] i18n translation loading is not thread-safe

3 participants