flask-session icon indicating copy to clipboard operation
flask-session copied to clipboard

Session failing when used with MongoDB due to datetime error

Open surathi opened this issue 8 years ago • 2 comments
trafficstars

I'm using session with MongoDB and it fails because of a datetime error. TypeError: can't compare offset-naive and offset-aware datetimes

  File "/Library/Python/2.7/site-packages/flask/app.py", line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1984, in wsgi_app
    ctx.push()
  File "/Library/Python/2.7/site-packages/flask/ctx.py", line 332, in push
    self.session = self.app.open_session(self.request)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 914, in open_session
    return self.session_interface.open_session(self, request)
  File "/Library/Python/2.7/site-packages/flask_session/sessions.py", line 411, in open_session
    if document and document.get('expiration') <= datetime.utcnow():
TypeError: can't compare offset-naive and offset-aware datetimes

I had recently faced a similar issue while checking expiry of a token. Handled it with using a different datetime format.

I believe we need to use any other format like: datetime.datetime.strptime(<timeStamp>,"%d %b %Y %I:%M:%S %p")

Mongo Version : 3.4 Flask-Session==0.3.0

surathi avatar Feb 09 '17 20:02 surathi

This issue was moved to mcrowson/flask-session#1

mcrowson avatar Feb 12 '17 00:02 mcrowson

It seems the mongodb python client returns an offset-aware datetime even though we put a offset-naive datetime in it.

fengsp avatar Apr 05 '17 03:04 fengsp

This was fixed in 0.6.0 and will be entirely eliminated from 0.7.0 by using time-to-live in mongo.

Lxstr avatar Feb 25 '24 07:02 Lxstr