1

From Flask's documentation, I have the following in my config:

<VirtualHost *>
    ServerName example.com

    WSGIDaemonProcess yourapplication user=user1 group=group1 threads=5
    WSGIScriptAlias / /var/www/yourapplication/yourapplication.wsgi

    <Directory /var/www/yourapplication>
        WSGIProcessGroup yourapplication
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

In my .wsgi file, I import the proper python file and import the flask app as application. Everything works fine, but I added logging to that file because I suspected something was wrong. Apparently, that wsgi file gets called every so often whenever a browser makes connection. It restarts the app (or at least a new process). I never noticed this, nor did I see it as a problem until I imported flask-login to manage authenticated sessions. Now whenever I login, after some short time, the wsgi app is reloaded and the session history no longer exists. In effect, I have to login every few seconds. Is this the intended way mod_wsgi works? I've tested my flask app running in standalone mode (flask's own devel server) and it works flawlessly.

In a way it's a duplicate, but it's also not. The server code isn't buggy. It's just mod_wsgi restarts the application over and over. Thanks for linking to the other post, though!

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
Jimmy
  • 65
  • 7
  • In a way it's a duplicate, but it's also not. The server code isn't buggy. It's just mod_wsgi restarts the application over and over. Thanks for linking to the other post, though! – Jimmy Jan 29 '18 at 19:31
  • I agree that it is not a duplicate. This one is about mod_wsgi restarting whole Flask app (e.g. it happens when user force reloads the webpage - wsgi starts a new process). – Peter Majko Nov 15 '18 at 17:55

1 Answers1

0

So what I found is that wsgi does restart the application every so often. I suppose this is expected behavior, but it's not what I expected. My issue with getting logged out was caused because I generate the app's secret key on startup. Therefore, the secret key was being changed constantly. Obviously this invalidates the cookies and logs a user out. So, I guess if you want to generate a secret key and not just have plain text in your source, you need to generate it once externally and import it into your flask application so that it doesn't always change.

Jimmy
  • 65
  • 7
  • 1
    If that really is your configuration, it should not be restarting the application all the time. Use check at http://modwsgi.readthedocs.io/en/develop/user-guides/checking-your-installation.html#embedded-or-daemon-mode to verify you really are using daemon mode. Despite what your configuration says, the behaviour you are seeing suggests you are using embedded mode, which can behave that way as Apache (not mod_wsgi) can recycle process. Also go read http://modwsgi.readthedocs.io/en/develop/user-guides/processes-and-threading.html – Graham Dumpleton Jan 30 '18 at 03:22
  • I would suggest you add ``WSGIRestrictEmbedded On`` outside of the virtual host to turn off embedded mode to ensure you are using daemon mode. Also set ``LogLevel info`` so mod_wsgi logs messages about daemon process restarts and why they are occurring. – Graham Dumpleton Jan 30 '18 at 03:24