Here's the setup:

  • Django (1.2) application on mod_wsgi that imports ctypes
  • Python 2.6.5
  • Apache 2.2.3
  • SELinux disabled
  • RedHat EL 5 64bit
  • a few of the file product is mounted over nfs

From time to time, after i restart apache I recieve an import error when it is attempting to import ctypes. Every incoming request fails having a 500 error. Basically restart apache usually everything just begins working again.

Here's the stack trace from the error:

Traceback (most recent call last):

 File "/home/appfirst/django/django/core/handlers/base.py", line 80, in get_response
   response = middleware_method(request)

     -------------- A BUNCH OF DJANGO MIDDLEWARE STUFF HERE -------------

 File "/home/appfirst/django/django/utils/importlib.py", line 35, in import_module
   __import__(name)

 File "/home/appfirst/backend/backend/streamer/views.py", line 6, in <module>
   import appfirst.main.models as FEmodels

 File "/home/appfirst/frontend/appfirst/main/models.py", line 27, in <module>
   import numpy, math, mpmath

 File "/usr/lib64/python2.6/site-packages/numpy/__init__.py", line 43, in <module>
   import ctypeslib

 File "/usr/lib64/python2.6/site-packages/numpy/ctypeslib.py", line 9, in <module>
   import ctypes

 File "/usr/lib64/python2.6/ctypes/__init__.py", line 546, in <module>
   CFUNCTYPE(c_int)(lambda: None)

MemoryError

I figured it may be associated with this bug, however i have SELinux switched off that we thought means this situation could never occur:

Any suggestions regarding how to reproduce it consistently and/or repair it? This is actually stumping me!

I have encounter this bug too. During my situation it happens after i professional a Python script from inside a PHP script running under Apache on the 64-bit A linux systemunix. [The Python code being run may be the front-finish to some pypy sandbox.] Exactly the same little bit of code works fine on the 32-bit system as well as works fine once the PHP script is performed from the command line. My "fix" continues to be only to comment out that line "CFUNCTYPE(c_int)(lambda: None)" in ctypes/init.py. It is the last type of the file and it is preceded through the following comment, showing the programmer does not understand what's happening, either!

# 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)

Clearly there is a much deeper problem somewhere in cpython, however the fix works best for me.

Consider turning SELinux off. It will solve the issue.