Django, mod_python, apache and wacky sessions

I am running a Django through mod_python on Apache on a linux box. I have a custom authentication backend, and middleware that requires authentication for all pages, except static content.

My problem is that after I log in, I will still randomly get the log in screen now and again. It seems to me that each apache process has it's own python process, which in turn has it's own internals. So as long as I get served by the same process I logged in to, everything is fine and dandy. But if my request gets served by a different apache process, I am no longer authenticated.

I have checked the HTTP headers I send with FireBug, and they are the same each time, ie. same cookie.

Is this a known issue and are there workarounds/fixes?

Edit: I have a page that displays a lot of generated images. Some off these will not display. This is because they are too behind the authenticating middleware, so they will randomly put up a login image. However, refreshing this page enough times, and it will eventually work, meaning all processes recognize my session.

Asked by: Roman299 | Posted: 28-01-2022

Answer 1

You are correct about how Apache handles the processes, and sometimes you'll get served by a different process. You can see this when you make a change to your site; new processes will pick up the change, but old processes will give you the old site. To get consistency, you have to restart Apache.

Assuming a restart doesn't fix the problem, I would guess it's something in the "custom authentication backend" storing part of the authentication in memory (which won't work very well for a web server). I would try setting MaxRequestsPerChild to 1 in your Apache config and seeing if you still get the login screen. If you do, something is being stored in memory, maybe a model not being saved?

Hope that helps!

P.S. Just out of curiosity, why are you using a custom authentication backend and a middleware to ensure the user is logged in? It seems Django's contrib.auth and @login_required would be easier...

Answered by: Marcus633 | Posted: 01-03-2022

Answer 2

Do you have standard database-driven sessions? Is caching enabled in settings?

Answered by: Emily454 | Posted: 01-03-2022

Answer 3

I highly recommend you don't set MaxRequestsPerChild to 1, as that would cause so much overhead as each process gets killed off and respawns with every request.

Are you using apaches preform MPM or worker MPM?

Take a look at that may give you some help

Answered by: Rafael705 | Posted: 01-03-2022

Answer 4

If you are using some global variables to hold data of your custom authentication session, you need to change this to use either file, database or memcached. As stated above mod_python launches few processes and there's no shared memory between them.

I recommend using memcached for this, also use cookies to store session ID or pass it with as GET parameter so that later you can easily extract session data from the cache.

Answered by: Blake602 | Posted: 01-03-2022

Answer 5

How to ensure that session is not cleared after Apache restart( or stop and start) ?

Because when I upgrade my source code and restart Apache, I refresh the web page and there I have to login again. Session is lost.

Session is stored in Memcache. No idea how and why its cleared. How to preserve the session so that the user need not login after the apache restart?

Answered by: Lily937 | Posted: 01-03-2022

Similar questions

python - Failed to get separate instances of a class under mod_python

I'm trying to run some python code under Apache 2.2 / mod_python 3.2.8. Eventually the code does os.fork() and spawns 2 separate long-run processes. Each of those processes has to create a separate instance of a class in order to avoid any possible collision in the parallel flow. class Foo(object): pass kidprocs = [] for kid in ('kid1', 'kid2'): pid = os.fork() if pid: # parent kidprocs.a...

python - Running Django with FastCGI or with mod_python

which would you recommend? which is faster, reliable? apache mod_python or nginx/lighttpd FastCGI?

python - How to access to the root path in a mod_python directory?

In my Apache webserver I put this: <Directory /var/www/> SetHandler mod_python PythonHandler mod_python.publisher PythonDebug On </Directory> Then I have a file with an index function. When I go to, I see a web page produced by the index function (just a plain vanilla HTML page). Every other page is of th...

python - How do I modify sys.path from .htaccess to allow mod_python to see Django?

The host I'm considering for hosting a Django site has mod_python installed, but does not have Django. Django's INSTALL file indicates that I can simply copy the django directory to Python's site-packages directory to install Django, so I suspect that it might be possible to configure Python / mod_python to look for it elsewhere (namely my user space) by modifying sys.path, but I don't know how to change it from .htaccess ...

php - mod_php vs mod_python

Why mod_python is oop but mod_php is not ? Example :We go to if you use mod_python apache opens www/ and calls dir2 method but if you use php module apache opens www/dir1/dir2/index.php

python - How do I upload a file with mod_python?

I want to create a simple file upload form and I must be completely incapable. I've read docs and tutorials,but for some reason, I'm not getting the submitted form data. I wrote the smallest amount of code I could to test and it still isn't working. Any ideas what's wrong? def index(): html = ''' <html> <body> <form id="fileUpload" action="./result" method="post"> ...

python - mod_python publisher and pretty URLs

I am new to Python (I am getting out of PHP because of how increasingly broken it is), and I am racing through porting my old code. One thing: I have a file / with functions index() and bar(), so, with the publisher I can access http://domain/foo/bar and http://domain/foo as the documentatio...

python - Configure Apache to recover from mod_python errors

This question already has an answer here:

python - mod_python caching of variables

I'm using mod_python to run Trac in Apache. I'm developing a plugin and am not sure how global variables are stored/cached. I am new to python and have googled the subject and found that mod_python caches python modules (I think). However, I would expect that cache to be reset when the web service is restarted, but it doesn't appear to be. I'm saying this becasue I have a global variable that is a ...

django - Different behavior of python logging module when using mod_python

We have a nasty problem where we see that the python logging module is behaving differently when running with mod_python on our servers. When executing the same code in the shell, or in django with the runserver command or with mod_wsgi, the behavior is correct: import logging logger = logging.getLogger('site-errors') logging.debug('logger=%s' % (logger.__dict__)) logging.debug('logger.parent=%s' % (logger....

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python