Recently, we attempted to bring up a new web server but to our dismay after it was all said and done, the Django application wouldn’t load. Eventually a “gateway timeout” was thrown with no errors in the wsgi or Apache error log. These types of errors are a pain to debug. What made the mystery even better, was the application ran just fine under the Django dev server!
I don’t think I need to go in depth into our debugging practices since that’s not what this post is really about. So I’m just going to skip ahead a little in the story.
Eventually we determined that by downgrading Pylibmc to 1.0 and libmemcached to 0.38 we could get the application to run fine under Apache. It really doesn’t make sense that upgrading should break “legacy” functionality. So I set out to determine the underlying cause.
After some trial and error with trying different version combinations of pylibmc, libmemcached and mod_wsgi, I finally decided to look at changelogs. Lo and behold, they held the answer I was looking for! (Go figure the easy way would have been faster)
Libmemcached 0.40-0.43 had nothing all that drastic.
Pylibmc 1.1 on the other hand had a change that seemed a little suspicious: “Acquire and release the GIL properly”
Sure enough, after digging into the mod_wsgi documentation I found out that it doesn’t play nice with the Python Simplified GIL State API (which is what pylibmc now uses in the “set” method and that line in the changelog references). It’s even referenced in the Python C-API docuemntation that it’s not supported in sub-interpreters!
The fix outlined by the mod_wsgi documentation says to run the application in the global interpreter by adding this to the configuration:
After adding that directive to the VirtualHost block in our Apache configuration; our Django application ran all just fine and dandy (and saves a little bit of memory). Woo!
For most applications running in the global interpreter is not going to cause any issues. However, according to mod_wsgi documentation this may cause issues when running multiple applications that access the same modules. This fix is definitely YMMV. So test THOROUGHLY!