6

I've got a setup with Apache + mod_wsgi running django code, and I'd like to add a layer of protection in case a non-terminating view slips in. Something that kills-off a requests exceeding, say, 30 seconds would be ideal.

For testing I've just put a time.sleep(60) in a view.

I've tried the TimeOut 30 setting in Apache, but I still get curl returning after 60 seconds.

I see mod_wsgi itself offers three different time-out values, but none of them seem to apply to a long-running request.

Is there a standard piece of Django middleware for this or is there a knob I'm missing on Apache or mod_wsgi?

1 Answer 1

6

It is actually really difficult to terminate a single Python request thread in a multithread application. The best you can do is make a decision to shutdown the whole process and restart it. Because such an action will disrupt concurrent requests, you would as a result really need to restrict yourself to a single threaded configuration.

Even then the support in mod_wsgi 3.X isn't ideal for this. There is inactivity-timeout for daemon mode, but it actually causes process to be restarted in two situations. The first is when there are no requests at all and process is idle. The second is when all request threads have blocked and timeout expires.

In mod_wsgi 4.X (in repository trunk at this time), the two concepts have been separated and now inactivity-timeout only applies to completely idle process with no concurrent requests. A new blocked-timeout has been added to separately specific a timeout for when whole process is blocked. It is this latter one you can use.

If you want to learn more about the new option, you will need to go over to the mod_wsgi mailing list to discusss it.

3
  • Thanks. One thing we tried to do was to switch from, say 5 processes with twenty request threads each, to 100 processes with one request thread each. The hope being that mod_wsgi's timeouts could then kill off processes w/o harming unrelated requests. In practice that didn't work out for system memory reasons (I would've expected more shared memory), but is it possible mod_wsgi could kill off that process if the inactivity-timeout triggers? Oct 12, 2011 at 4:58
  • The application is loaded after the fork, so nothing is shared such that copy on write would be a benefit. The benefit of preloading before forking in Python isn't as much as people would think as code execution manipulates reference counts and so copy is made on most stuff any way. Oct 12, 2011 at 9:31
  • The blocked-timeout option in mod_wsgi 4.0 is still probably going to be your best bet. You can still run multithreaded and it will kick in when all threads have got stuck. Waiting until all threads stuck though isn't good either. So there is also a blocked-requests setting. If you have threads=15, you can say blocked-requests=5. So as soon it gets five threads stuck and hits point where no requests being handled by process either, then it will safely do a restart. Being able to set the blocked requests threshold gives you a safety margin so process doesn't bog down. Oct 12, 2011 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.