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

Fix locale selection in multi-threaded Flask app when force_locale is used #125

Merged
merged 1 commit into from
Sep 23, 2019
Merged

Fix locale selection in multi-threaded Flask app when force_locale is used #125

merged 1 commit into from
Sep 23, 2019

Conversation

lautat
Copy link
Contributor

@lautat lautat commented Oct 8, 2017

This fixes issue #117 by setting babel_locale in force_locale context manager instead of replacing locale getter. Babel extension instance is shared across threads (through Flask.extensions) which may cause wrong locale to be selected in request that is processed at the same time in one thread while another thread is using force_locale. Setting locale to request context is thread-safe as Flask doesn't share request contexts between threads. I've added a test case to verify that this fix works, and it failed without changes to force_locale.

@lautat lautat changed the title Fix locale selection in multi-threaded Flask app Fix locale selection in multi-threaded Flask app when force_locale is used Oct 8, 2017
@tvuotila
Copy link

tvuotila commented Nov 2, 2018

Calling flask_babel.refresh() will revert the force_locale prematurely.

@lautat
Copy link
Contributor Author

lautat commented Nov 2, 2018

@tvuotila thanks for the feedback. Since (I assume) your fix works with flask_babel.refresh(), I can close this pull request. This fix is not critical for me since I've applied a workaround to the project which was affected by this bug. I can also try to reproduce the issue with flask_babel.refresh() and fix my patch.

@tvuotila
Copy link

tvuotila commented Nov 6, 2018

Since (I assume) your fix works with flask_babel.refresh(), I can close this pull request.

Yes, my fix works with flask_babel.refresh().

I can close this pull request.

I like your solution more. It does not introduce another place for locale_selector_func, like mine does.

I can also try to reproduce the issue with flask_babel.refresh() and fix my patch.

If you could, that would be great.

@lautat
Copy link
Contributor Author

lautat commented Nov 6, 2018

Alright, I'll fix the patch.

This fixes issue #117 by setting locale in `force_locale` instead of
replacing locale getter. Babel extension instance is shared across
threads which causes issues when `force_locale` is used in one request
while another starts. `forced_babel_locale` attribute of context is used
to override `babel_locale` in case `refresh` is called within
`force_locale` context.
@lautat
Copy link
Contributor Author

lautat commented Nov 6, 2018

I fixed the patch. It is not very pretty, but it works now with flask_babel.refresh().

@Dreamsorcerer
Copy link

After spending 3 days trying to figure why our site randomly provides Chinese translations to all our users, we found this PR. This has resolved the issue and should be merged into the next release, so others don't have to go through the same troubles.

@lautat
Copy link
Contributor Author

lautat commented Sep 18, 2019

Perhaps @TkTech could take a look? It looks like he has been maintaining this project lately.

@TkTech
Copy link
Contributor

TkTech commented Sep 20, 2019

@lautat I can take a look this weekend. Keep in mind of the 4 people with admin access to this repo (which doesn't include me), none of them have ever committed or commented. I can't protect branches and add others as reviewers, nothing. So it's unfortunately just me vs the backlog and I no longer use flask at work. @mitsuhiko has unfortunately never replied to any of my emails over the years :(

@TkTech TkTech changed the base branch from master to dev September 23, 2019 02:54
@TkTech TkTech merged commit d5abdd1 into python-babel:dev Sep 23, 2019
@lautat lautat deleted the fix/thread-safe-force-locale branch September 23, 2019 06:48
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.

None yet

4 participants