7

I've set up my Python/Django virtual environment, and mod_wsgi in daemon mode, and am pretty sure (done this before) it's "mostly correct" except I get the following error...

[Thu Jul 06 00:35:26.986363 2017] [mpm_event:notice] [pid 11442:tid 140557758930432] AH00493: SIGUSR1 received.  Doing graceful restart
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
  File "/home/jamin/www/dev.tir.com/py361ve/lib/python3.6/site-packages/PIL/Image.py", line 572, in __del__
NameError: name 'hasattr' is not defined
[Thu Jul 06 00:35:27.194483 2017] [mpm_event:notice] [pid 11442:tid 140557758930432] AH00489: Apache/2.4.25 (Ubuntu) mod_wsgi/4.5.15 Python/3.6 configured -- resuming normal operations
[Thu Jul 06 00:35:27.194561 2017] [core:notice] [pid 11442:tid 140557758930432] AH00094: Command line: '/usr/sbin/apache2'

My django app itself is loading fine through wsgi.py but it seems something to do with core python (error with my setup likely) is going wrong as per: NameError: name 'hasattr' is not defined

In the browser - I get a plain "Server Error (500)" page and not the standard Apache "Internal Server Error" page.

Leaving out my VirtualHost and steps beyond here are the basic steps I put together for myself if you can spot anything... (I've tried all the different python packages as well not just -venv)

Install Python 3.6 and virtualenv
    sudo apt-get update
    sudo apt-get install python3.6-venv
    sudo apt-get install virtualenv
(or find the latest and greatest python package that includes pip https://packages.ubuntu.com/ )


Install Apache2
    sudo apt-get install apache2 apache2-dev


Make and enter a folder for your project - then build a Virtual Environment in it
    mkdir ~/example.com
    cd ~/example.com
    virtualenv --python=/usr/bin/python3.6 py361ve

Enter your new Virtual Environment to install packages to it
    source py361ve/bin/activate

Install Django, mod_wsgi, and any other needed packages
    pip install django
    pip install mod_wsgi
    pip install ...
(no need for pip3 in virtual environment - django should be the latest release)


Run following command and place output in apache config file ( in /etc/apache2/ )
    mod_wsgi-express module-config


Exit your virtual environment
    deactivate
(You can re-enter your virtual environment any time using the source method in step 8)

Here's what happens when I stop/start/restart apache2...

apache2 stop...

[Thu Jul 06 06:01:34.190940 2017] [mpm_event:notice] [pid 2015:tid 140157449797120] AH00491: caught SIGTERM, shutting down
_______________________________________________________________
apache2 start...

[Thu Jul 06 06:02:39.076741 2017] [mpm_event:notice] [pid 2181:tid 140553545080320] AH00489: Apache/2.4.25 (Ubuntu) mod_wsgi/4.5.15 Python/3.6 configured -- resuming $
[Thu Jul 06 06:02:39.076890 2017] [core:notice] [pid 2181:tid 140553545080320] AH00094: Command line: '/usr/sbin/apache2'
_______________________________________________________________
apache2 restart...

Exception ignored in: <object repr() failed>
Traceback (most recent call last):
  File "/home/jamin/www/dev.tir.com/py361ve/lib/python3.6/site-packages/PIL/Image.py", line 572, in __del__
NameError: name 'hasattr' is not defined
[Thu Jul 06 06:05:43.307877 2017] [mpm_event:notice] [pid 2181:tid 140553545080320] AH00491: caught SIGTERM, shutting down
[Thu Jul 06 06:05:43.492499 2017] [mpm_event:notice] [pid 2301:tid 140353155558912] AH00489: Apache/2.4.25 (Ubuntu) mod_wsgi/4.5.15 Python/3.6 configured -- resuming $
[Thu Jul 06 06:05:43.492705 2017] [core:notice] [pid 2301:tid 140353155558912] AH00094: Command line: '/usr/sbin/apache2'
JxAxMxIxN
  • 1,571
  • 1
  • 16
  • 20
  • 1
    In my case it turned out to be a completely unrelated problem: when I turned the logging on, I discovered that django was issuing the 500 code because a problem with the template URLs. Once solved, the page started to behave properly. The `hasattr` error was a red herring for me. – user2658323 Feb 02 '18 at 15:23
  • 1
    @user2658323 Thank you so much! I have spent about 3 hours debugging this issue before I found your comment -- it turns out, my issue was also completely unrelated to Pillow, and it would have been obvious had `DEBUG` been set to True in my `settings.py`... – martonbognar Apr 04 '18 at 15:37
  • 1
    @martonbognar - yup that DEBUG = true option in settings saved me in this case as well. I instantly found my problem! – JxAxMxIxN Apr 05 '18 at 20:49
  • 1
    In my case it was caused by SELinux preventing apache to have access to file. – dismine Feb 24 '19 at 14:30
  • Dismine - I think file access had something to do with my issue too. That seems to be the problem a lot of the time in fact – JxAxMxIxN Feb 25 '19 at 17:16

3 Answers3

5

This is likely due to code still running in background threads when the Python interpreter is being destroyed on process shutdown. What happens during interpreter destruction is that all modules get wiped out and attribute access of things often returns None as fallback. In this case looks like builtins module was wiped before the PIL object got destroyed and so it couldn't find hasattr.

Can you confirm this only happens when Apache is being restarted and processes shutdown?

Graham Dumpleton
  • 54,716
  • 6
  • 106
  • 126
  • I ran some tests, and posted the results in the original post. I hope it helps - can't wait to find out what stupid thing I'm doing wrong here (usually the case) – JxAxMxIxN Jul 06 '17 at 06:13
  • 2
    You aren't doing anything stupid, it just a thing that can happen when shutting down processes and there are daemon threads still running. If you do not see the issue in the normal operation of your server you can ignore it. The question still is whether you are creating background threads to run any tasks after a request completes. In general using threads to run tasks after a request has completed is a bad idea. Better to use a separate task queueing system such as Celery. – Graham Dumpleton Jul 06 '17 at 06:30
  • 1
    Yes I was - I never once set `DEBUG = True` in Django settings after clearing all my other issues... If I had I would've found the issue in my code and realized everything else was set up and functioning properly and never written this post in the first place. What keeps you from cursing us out Graham? – JxAxMxIxN Jul 06 '17 at 07:08
  • Settings ``DEBUG=True`` wouldn't help change the fact that this issue can occur and there usually isn't a lot you can do about it. Only part solution is to register ``atexit`` callbacks which fire on process shutdown and you try and stop the threads first. If the threads are not specifically designed to look for some shutdown signal flagged by the callbacks, doesn't help. – Graham Dumpleton Jul 06 '17 at 07:43
1

I encountered the same problem as well.

[Wed Sep 12 16:30:19 2018] [notice] Apache/2.2.15 (Unix) DAV/2 mod_wsgi/4.6.4     
Python/3.6 configured -- resuming normal operations
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
  File "/mnt/workshop/py36env/lib/python3.6/site-packages/PIL/Image.py", line 600, in __del__
NameError: name 'hasattr' is not defined
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
  File "/mnt/workshop/py36env/lib/python3.6/site-packages/PIL/Image.py", line 600, in __del__
NameError: name 'hasattr' is not defined
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
  File "/mnt/workshop/py36env/lib/python3.6/site-packages/PIL/Image.py", line 600, in __del__
NameError: name 'hasattr' is not defined
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
  File "/mnt/workshop/py36env/lib/python3.6/site-packages/PIL/Image.py", line 600, in __del__
NameError: name 'hasattr' is not defined
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
  File "/mnt/workshop/py36env/lib/python3.6/site-packages/PIL/Image.py", line 600, in __del__
NameError: name 'hasattr' is not defined
[Wed Sep 12 16:33:57 2018] [notice] caught SIGTERM, shutting down
[Wed Sep 12 16:33:58 2018] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)

It maybe happened when you made a new dir but never authorized.

And I fixed it by the following:

chmod -R 755 /mnt/workshop/your_project_path
chown -R apache:apache /mnt/workshop/your_project_path
Pang
  • 8,605
  • 144
  • 77
  • 113
fancyChuan
  • 11
  • 1
0

Try uninstalling PIL completely, and then reinstall Pillow package.

Actually there is conflict in PIL old package and Pillow package which is in conflict with newer versions of django.

You need to install the new Pillow package.

First Uninstall both

sudo pip3 uninstall pil
sudo pip3 uninstall pillow

Reinstall Pillow

sudo pip3 install pillow
Astik Anand
  • 10,975
  • 9
  • 28
  • 42
  • I tried this in my virtual environment and in my regular install. I could uninstall/reinstall pillow in my virtual, but not pil... I don't have any PIL/Pillow issues in my Windows test environment (only using django server). Maybe it's a deeper issue... – JxAxMxIxN Jul 06 '17 at 06:29