Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ctypes MemoryError in fcgi process from PIL library

I'm trying to run Django on shared hosting (Bluehost). I'm using functionality that requires PIL. PIL imports and works from an interactive shell, but in my fcgi process it crashes with a MemoryError at from PIL import image. Any help on why it might be failing inside fcgi would be much appreciated.

__Environment Info__:  
Python2.7

Local installs of libjpg, zlib, freetype, and lcms

Virtualenv:  
Django 1.3, PIL, flup, etc.

__Stack Trace__:  

    File ".../feincms_thumbnail.py", line 3, in <module>  
        from PIL import Image

    File ".../PIL/Image.py", line 45, in <module>  
        \__import__("FixTk")

    File ".../python2.7/lib-tk/FixTk.py", line 15, in <module>  
        import ctypes

    File ".../python2.7/ctypes/__init__.py", line 549, in <module>  
        CFUNCTYPE(c_int)(lambda: None)

__.fcgi__:  

<!-- language: python -->
    # setup paths
    # set DJANGO_SETTINGS_MODULE in os.environ  

    from django.core.servers.fastcgi import runfastcgi  
    runfastcgi(method="threaded", daemonize="false")
like image 623
Peter Avatar asked May 06 '11 17:05

Peter


3 Answers

I have temporarily fixed that error commenting the last line in this file $HOME/lib/python2.7/ctypes/__init__.py that is something like #CFUNCTYPE(c_int)(lambda: None).

That's work for me, but i don't know what exactly the problem is.

UPDATE

In python 2.7.3 the line number is :279 not the last as I said above.

UPDATE 2 Since the line may vary between minor versions, you should look for a chunk of code that looks something like this:

# XXX for whatever reasons, creating the first instance of a callback
# function is needed for the unittests on Win64 to succeed.  This MAY
# be a compiler bug, since the problem occurs only when _ctypes is
# compiled with the MS SDK compiler.  Or an uninitialized variable?
CFUNCTYPE(c_int)(lambda: None)
like image 58
eos87 Avatar answered Nov 08 '22 04:11

eos87


try running this command:

setsebool -P httpd_tmp_exec on

fixes things for me on CentOS. Taken from this post: https://bugzilla.redhat.com/show_bug.cgi?id=645193

like image 45
Adam Spence Avatar answered Nov 08 '22 05:11

Adam Spence


Just to expand on eos87's answer a bit, this does fix the problem for me as well, and judging by the comment before that line, it sounds like it was added as a workaround to a windows bug, but the workaround is apparently causing trouble of its own. Here's the bit at the end of __init__.py:

# XXX for whatever reasons, creating the first instance of a callback
# function is needed for the unittests on Win64 to succeed.  This MAY
# be a compiler bug, since the problem occurs only when _ctypes is
# compiled with the MS SDK compiler.  Or an uninitialized variable?
CFUNCTYPE(c_int)(lambda: None)

It looks like it's safe to remove.

FWIW, this issue showed up for me on a Centos 5.7 x64 box when using python 2.6 as installed (in parallel with python 2.4) from epel. The file was found here: /usr/lib64/python2.6/ctypes/__init__.py

Also note that the exception that shows up is a MemoryError which according to strace results from a segmentation fault immediately (though perhaps coincidentally) after a call to munmap; and it only shows up when running as FastCGI.

like image 27
tylerl Avatar answered Nov 08 '22 04:11

tylerl