python-babel / flask-babel

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

Store forced locale per request #146

Closed tvuotila closed 4 years ago

tvuotila commented 5 years ago

Allow multiple request to share the same application.

Problem: We are running Flask with Gunicorn with gevent worker type. This means, that when a request processing is waiting for an i/o, another request processing can start.

In my tests, I managed to have two requests use the same application object through current_app proxy. This introduced the following bug:

  1. Request A enters force_locale block, which replaces babel.locale_selector_func with lambda A. Original locale_selector_func is saved.
  2. Request A does some i/o work.
  3. Request B is started. Request B enters force_locale block, which replaces babel.locale_selector_func with lambda B. Lambda A is saved.
  4. Request B does some i/o work.
  5. Request A finishes the i/o work and exits force_locale block. babel.locale_selector_func is replaced with the the original locale_selector_func.
  6. Request B finishes the i/o work and exits force_locale block. babel.locale_selector_func is replaced by lambda A.

Now babel.locale_selector_func is lambda A instead of the original locale_selector_func.

My solution to the problem: Store forced locale_selector_func per request. Try to get locale_selector_func first from the request info and fall back to the one stored in the application object.

quantus commented 5 years ago

@tvuotila: Isn't this the same as #125? Can you check if these changes are identical, or if there are any differences?

tvuotila commented 5 years ago

@tvuotila: Isn't this the same as #125? Can you check if these changes are identical, or if there are any differences?

They solve the same problem, but I notice an issue there.

tvuotila commented 4 years ago

I close this because #125 is better