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

session.clear() removes expiry field, causing TypeError

Open grutz opened this issue 8 years ago • 4 comments

Setup: Flask 0.11, SQLAlchemy session interface, python 2.7.x

When using session.clear() to empty a session, say on a logout signal to ensure an empty session record, the expiry field is set to null in the database:

dev=# select session_id,expiry from sessions;
                  session_id                  | expiry
----------------------------------------------+--------
 session:ed53619f-84d7-4985-9383-260aa9817125 |
(1 row)

This causes a TypeError exception on line 516:

    if saved_session and saved_session.expiry <= datetime.utcnow():
        # Delete expired session
        self.db.session.delete(saved_session)
        self.db.session.commit()
        saved_session = None

Work-around solution is to check for saved_session.expiry before comparison:

    if saved_session and (not saved_session.expiry or saved_session.expiry <= datetime.utcnow()):

grutz avatar Jan 20 '17 21:01 grutz

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

mcrowson avatar Feb 12 '17 00:02 mcrowson

I don't understand why expiry is null here :(

fengsp avatar Apr 05 '17 03:04 fengsp

I'm having the same problem.

johnnymetz avatar May 31 '17 21:05 johnnymetz

Same...

Tethik avatar Jul 24 '17 19:07 Tethik

This was an issue where clear() was also clearing the _permanent in the session dict. Fixed post 0.7.0rc1

Lxstr avatar Mar 10 '24 13:03 Lxstr