9

I am running flask, pymongo and flask-login as a stack.

My flask app is running fine locally, but once I deploy it with uwsgi on nginx, I get a strange unicode error from flask_login extension.

In short:

TypeError: decoding Unicode is not supported

Traceback:

[pid: 21753|app: 0|req: 5/5] 84.207.253.34 () {38 vars in 600 bytes} [Thu Jun 13 16:51:08 2013] GET / => generated 0 bytes in 4 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)
Traceback (most recent call last):
  File "/myproject/myproject-env/local/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/myproject/myproject-env/local/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/myproject/myproject-env/local/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/myproject/myproject-env/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/myproject/myproject-env/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/myproject/myproject-env/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/myproject/myproject-env/local/lib/python2.7/site-packages/flask/app.py", line 1473, in full_dispatch_request
    rv = self.preprocess_request()
  File "/myproject/myproject-env/local/lib/python2.7/site-packages/flask/app.py", line 1666, in preprocess_request
    rv = func()
  File "/myproject/myproject-env/local/lib/python2.7/site-packages/flask_login.py", line 311, in _load_user
    deleted = self._session_protection()
  File "/myproject/myproject-env/local/lib/python2.7/site-packages/flask_login.py", line 325, in _session_protection
    ident = _create_identifier()
  File "/myproject/myproject-env/local/lib/python2.7/site-packages/flask_login.py", line 133, in _create_identifier
    request.headers.get("User-Agent")), 'utf8', errors='replace')
TypeError: decoding Unicode is not supported

Why is this not happening in dev environment? Hence it must be somehow related to uwsgi on nginx. Any suggestions? Many Thanks

Houman
  • 58,044
  • 75
  • 235
  • 407

4 Answers4

17

The problem won't be solved by downgrading flask alone, because even installing flask==0.9 would install the latest dependencies, which is the bad werkzeug==0.9 Hence you better install the following in this order:

pip install werkzeug==0.8.3
pip install flask==0.9
pip install Flask-Login==0.1.3

flask login, can then be the latest version 0.1.3. No harm done there. This stack works for me.

Hope this helps, until the emergency patch is out.

Houman
  • 58,044
  • 75
  • 235
  • 407
  • 3
    In my opinion, this is the best thing to do. – maxcountryman Jun 14 '13 at 01:02
  • Re: the emergency patch: considering the maintainer of Flask-Login has [said not to expect one](https://github.com/maxcountryman/flask-login/issues/78#issuecomment-19428468), I would not expect it to come out. From the looks of it, Flask-Login will not support Flask v0.10 until a cleanup branch. Which, AFAIC, is fine: just don't use Flask 0.10 yet :) – Mark Hildreth Jun 14 '13 at 22:27
  • Also note this is an upstream bug, [reported here](https://github.com/mitsuhiko/flask/issues/772). Flask-Login has been updated but cannot address the issue upstream, of course. You may prefer to use the [0.2.x release series](https://github.com/maxcountryman/flask-login/tree/0.2.2) now. – maxcountryman Jun 17 '13 at 18:52
  • Unfortunately, this doesn't work for me, though I suspect it's Flask-OauthLib that is at issue. – tsalaroth Jun 01 '14 at 14:05
7

I am having the very same problem on my dev environment, with Flask 0.10 and Flask-Login 0.1.3

looks like flask 0.10 now has unicode request headers so flask-login explodes when trying to encode an already encoded string...

Flask_login people are already working on it: https://github.com/maxcountryman/flask-login/issues/78

(EDIT) instant road to temporary happiness (as seen in github twin thread, thx Kofalt & Kave!)

pip uninstall flask ; pip uninstall werkzeug ; pip uninstall Flask-Login ; pip install werkzeug==0.8.3 ; pip install flask==0.9 ; pip install Flask-Login==0.1.3
Iosu S.
  • 154
  • 10
  • Thanks for the link. I have now deleted the entire virtual-env and created a new stack. But I still get the same error: `Babel==0.9.6 Flask==0.9 Flask-Assets==0.8 Flask-Babel==0.8 Flask-Bcrypt==0.5.2 Flask-Login==0.1.3 Flask-WTF==0.8.3 Jinja2==2.7 MarkupSafe==0.18 WTForms==1.0.4 Werkzeug==0.9 argparse==1.2.1 closure==20121212 cssmin==0.1.4 isodate==0.4.9 jsonschema==2.0.0 py-bcrypt==0.3 pymongo==2.5.2 python-dateutil==1.5 pytz==2013b rauth==0.5.4 requests==1.1.0 speaklater==1.3 webassets==0.8 wsgiref==0.1.2` – Houman Jun 13 '13 at 22:18
  • Same here Kave, maybe werkzeug has to be downgraded too? a emergency fix for flask-login has been requested however. – Carst Jun 13 '13 at 22:24
  • Use Flask 0.9 until there is a release or alternatively patch Flask-Login yourself. – maxcountryman Jun 14 '13 at 01:01
2

My fork which fixes this issue:

https://github.com/jgelens/flask-login/tree/0.1.4

Install using:

pip install https://github.com/jgelens/flask-login/archive/0f07b8fa783c40d09cb284d442a526f067bab28b.zip#egg=flask-login
1

As per losu S., this looks to be a Flask 0.10 problem. Try to install previous version of Flask in your virtual environment using:

pip install Flask==0.9
Mark Hildreth
  • 36,998
  • 9
  • 113
  • 105
marcoseu
  • 3,765
  • 2
  • 14
  • 32
  • 1
    `pip install Flask==0.9` don't resolve problem because it will download `werkzeug==0.9` so you must use `pip install flask==0.9 werkzeug==0.8.3` – tbicr Jun 14 '13 at 11:06