Random ctypes error importing numpy from django mod_wsgi application

Here's the setting:

  • Django (1.2) application for mod_wsgi that imports ctypes
  • Python 2.6.5
  • Apache 2.2.3
  • SELinux is disabled
  • Redhat EL 5 64bit
  • part of the file system is mounted on top of nfs

Sometimes, when I restart apache, I get an import error when trying to import ctypes. Each incoming request fails with error 500. If I restart apache, usually everything starts working again.

Here's the error stack trace:

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 thought that this could be due to this error, but I have SELinux turned off, which I thought would mean that this case can never happen:

Any suggestions on how to reproduce it sequentially and / or fix it? It really shocks me!

+1
source share
2 answers

I also ran into this error. In my case, this happens when I execute a Python script from a PHP script that runs under Apache on a 64-bit Linux system. [Python executable code is the interface for the pypy sandbox.] The same bit of code works fine on a 32-bit system and even works fine when a PHP script is executed directly from the command line. My “fix” simply commented on this line “CFUNCTYPE (c_int) (lambda: no)” in ctypes / init .py. This is the last line of the file and it is preceded by the following comment, showing that the programmer does not understand what is 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 deeper problem in cpython, but the fix works for me.

+8
source

Consider disabling SELinux. This should solve the problem.

-2
source

Source: https://habr.com/ru/post/913602/


All Articles