pallets-eco / flask-session

Server side session extension for Flask
https://flask-session.readthedocs.io
BSD 3-Clause "New" or "Revised" License
507 stars 238 forks source link

Session failing when used with MongoDB due to datetime error #63

Closed surathi closed 8 months ago

surathi commented 7 years ago

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(,"%d %b %Y %I:%M:%S %p")

Mongo Version : 3.4 Flask-Session==0.3.0

mcrowson commented 7 years ago

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

fengsp commented 7 years ago

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

Lxstr commented 8 months ago

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