-
-
Notifications
You must be signed in to change notification settings - Fork 410
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
Value of I18n.locale can leak between requests in a multi-threaded environment/server #381
Comments
Thanks @shayonj for the excellent steps to reproduce. I did what you said to do, but I was even able to reproduce it by loading the |
Simple way to refresh fast:
|
We are also seeing this in production |
Thanks for the confirmation @brightchimp. Does #382 fix this for you? |
Hi @radar, we are just debugging at the moment and will try to test that PR next week. |
Hi @radar - Apologies, I think our issue is related to this, but is of our own making. We explicitly set We think that because the locale is not being reset (as per this issue), the call to |
Ok; no worries @brightchimp. Thanks for letting me know. |
Closing this as I think it is fixed by #382. |
Hi, I got this problem in development today with a very new rails 5.2 project with puma and no code that changes locale that I know of. (Unless It seemed to pick a random locale from available_locales. I could then get a different one whenever I refreshed the page or did a curl request. But then I found this and tried the I18n::Middleware, which seems to solve it ⭐️ . |
@scmx in case you still having this issue, see rails/rails#34356 |
What I tried to do
I am working with a high concurrency rails app. The application serves content to requests based on the
locale
params orHTTP_ACCEPT_LANGUAGE
header that is received specific to that request. The controller action does something like this before a new request:However, I have witnessed requests not specific to that action return content in a locale that is not the default locale (default locale is
en
).What I expected to happen
The expectation is: On a new request, if
I18n.locale
is not being set to anything, it should return the default locale.Versions of i18n, rails, and anything else you think is necessary
0.8.6
5.1.3
3.10.0
Replication steps
I have setup a test application here: https://github.com/shayonj/i18n-test-repro. You can replicate this issue by performing:
bundle install
rails s
(This application runsPuma
with max 5 threads.)Tab1
) openhttp://localhost:3000?locale=es
MainController#index
setsI18n.locale
toparams[:locale]
if present one orI18n.default_locale
Tab2
) openhttp://localhost:3000/foo
I18n.locale
and expects it to returnen
(the default locale)Tab2
the current locale being rendered is no longeren
butes
Here is a screen capture demonstrating the above: https://www.dropbox.com/s/zbogg3vag5at98b/i18n-locale-leak.mov?dl=0
Additional info
I can see an argument being made that every action should have something like
I18n.locale = :en
in abefore_action
block. But then I guess, thats what the point is for definingdefault_locale
toI18n
config as part of an Initializer:Proposal for a solution
As a work around, in our app, I am using a custom middleware that resets
Thread.current[:i18n_config]
after each request. The heart of the problem appears to be that TLS isn't cleared between requests and some how it gets persisted or shared via a Thread pool or similar implementation (common between servers like Puma/Unicorn or Passenger (ENT) with a concurrency model ofthreads
).That being said, the above workaround has solved the issue for us and I'd like to propose/discuss if a similar thing is possible to have in the
i18n
gem itself? That is, to have something like aI18n::Middleware
that any rack based can utilize or plugin to their app itself (out of the box), instead of having to write/maintain one natively. Or perhaps a different solution altogether?The text was updated successfully, but these errors were encountered: