Flask session not persistent across requests in Flask app with Gunicorn on Heroku Flask session not persistent across requests in Flask app with Gunicorn on Heroku heroku heroku

Flask session not persistent across requests in Flask app with Gunicorn on Heroku


Looks like there were two problems:

  • The app.secret_key shouldn't be set to os.urandom(24) because every worker will have another secret key
  • For some reason the dict where I stored my sessions in was sometimes empty and sometimes not... Still haven't found the reason for this though

Storing the sessions in a database instead a dictionary at runtime solves the problem.


I had a similar issue, but for me the answer was related to the cookies. A new session was being created when I opened my development environment, then another one when going to google, and a new one after a successful log in.

The problem was that my SESSION_COOKIE_DOMAIN was incorrect, and the cookie domain was being set to a different host. For my local development purposes I set SESSION_COOKIE_DOMAIN = '127.0.0.1', and use http://127.0.0.1: to access it, and it works OK now.