python-babel / flask-babel

i18n and l10n support for Flask based on Babel and pytz
https://python-babel.github.io/flask-babel/
Other
444 stars 159 forks source link

inter-thread force_locale interference #117

Closed rnd0101 closed 4 years ago

rnd0101 commented 7 years ago

There seems to be a problem with Flask-Babel==0.11.1 in multithreaded Flask app. It can be revealed (even in threaded werkzeug, but first encountered in mod_wsgi env) by adding debug point to https://github.com/python-babel/flask-babel/blob/master/flask_babel/__init__.py#L337 (just after "temporarily" changing babel's locale_selector_func). Then application, accessed from another thread, is stuck with lambda: locale, whatever locale happened to be.

If it's not easy to fix, it should be properly indicated, that context manager in question is not thread-safe.

Flask is 0.10.1.

(also: http://stackoverflow.com/questions/43470984/flask-babel-not-thread-safe )

rnd0101 commented 7 years ago

More info: the described problem seems to coincide with C-module code failures (in my case pylibmc under higher concurrency), so maybe try-finally has no chance to execute finally part. Could be nice to have some function to recover babel in the flask from such mishaps.

Dreamsorcerer commented 5 years ago

This is a critical issue. We've just spent 3 days trying to figure out why our production website has been randomly serving Chinese translations to all users.

We've monkey-patched the force_locale() function using the code in the attached PR, and this has resolved the issue. Please consider this tested and get this merged into the next release, so other users don't have to waste the time trying to figure this out.

TkTech commented 4 years ago

The fix for this has been live for quite awhile, but I neglected to close this ticket.