0

I'm building a webapp using django 1.7 on python 2.7, deployed in production with apache and mod_wsgi in daemon mode. I'm having a problem that wsgi hangs whenever I have an exception logged (with python logging) and the AdminEmailHandler configured in my settings.py.

When I request the page the browser hangs until eventually the apache log shows "End of script output before headers: wsgi.py"

I have two logging handlers configured, a FileHandler and AdminEmailHandler, and when I comment out the AdminEmailHandler the problem doesn't occur.

'handlers': {
    'file': {
        'level': 'INFO',
        'class': 'logging.FileHandler',
        'filename': '/var/log/apache2/mylog',
        'formatter':'simple',
    },
    'email': {
        'level': 'ERROR',
        'class': 'django.utils.log.AdminEmailHandler',
        'formatter':'simple',
    }

I have seen that I could configure wsgi with

WSGIApplicationGroup %{GLOBAL}

and that there are application issues with mod_wsgi (from this question).

However I would like to keep daemon mode if possible, and the AdminEmailHandler seems to be a core piece of the logging functionality that I'm surprised this hasn't been encountered before!

Thanks in advance for your help

UPDATE

I fixed the issue with my own subclass of AdminEmailHandler that delegates the email sending to a separate thread. Thanks to Vinay for the help!

from threading import Thread
import django.utils.log as djangolog
class MyAdminEmailHandler(djangolog.AdminEmailHandler):
    def emit(self, record):
        try:
            thread = Thread(target=djangolog.AdminEmailHandler.emit, args=(self, record))
            thread.start()
        except:
            self.handleError(record)
Community
  • 1
  • 1

1 Answers1

0

I don't believe this is especially related to daemon mode - it's more likely that SMTP is blocking your web app. See this question and all the answers for a possible solution.

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
  • Thanks for your help I will give one of those solutions a go – user3767915 Jun 24 '14 at 15:44
  • Thanks that was the cause of the problem, the thread being blocked by sending the message. I solved the problem with my own subclass of AdminEmailHandler. I've updated the question to reflect the solution – user3767915 Jun 25 '14 at 09:25