1

These IOErrors are fairly well known, as seen in other SO threads (see below), and can be caused by a user closing their browser mid-upload. For a discussion of why there is no simple solution, see this django-developers thread.

I first tried the middleware option described in an answer to this ioerror SO thread (except in our case I would branch on the request uri). It didn't work: I could capture the response and see that as far as Django was concerned, there wasn't a file included in the post, and it was returning a 200 response with the validation error. And yet, I can still see the IOError in Apache's error log.

Next we found this discussion on a django bug which suggests you should suppress this with a Logging filter. I tried attaching the filter to our custom logger, the root logger and 'django.request' ... I still can't see the IOError to suppress it! I can only see log messages associated with the failed validation of the form.

Any suggestions?

[ Edit: here is my setup ]

In settings.py (mostly copied from Django's conf/global_settings.py):

DEBUG = False

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'ioerrsquash': {
            '()': 'es.ioerr_filter.IOErrorFilter'
        }    },
    'handlers': {
            'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'filters': ['ioerrsquash']
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'wsgi.errors': { # added this logger after django.request failed
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
       }
    }
}

Content of ioerr_filter.py (obviously just for testing purposes):

import logging
import sys
from django.conf import settings

class IOErrorFilter(logging.Filter):
    def filter(self, record):
        LOGF = "%s/BU_IOERR.log" % settings.LOG_FILE_DIR
        f = open(LOGF, 'a')
        f.write('... record\n')
        f.write(str(record))
        f.write('\n... exc_info\n')
        f.write(str(sys.exc_info()))
        f.write('\n...\n')
        return True
Community
  • 1
  • 1
EMiller
  • 1,138
  • 8
  • 23

1 Answers1

0

There are more recent issues where this is now raised as an UnreadablePostError on trunk/1.4beta. See https://code.djangoproject.com/ticket/17277 and https://code.djangoproject.com/ticket/17069. You should be able to use a logging filter to suppress these errors. There is an example in #17069.

Mark Lavin
  • 24,664
  • 5
  • 76
  • 70
  • Thanks Mark, yes I used #17069 as my template for the logging filter. As I mentioned I attached the filter to root and two named loggers (django.request) - but I'm not seeing the error come through. In my testing I removed all conditions (eg. `def filter(self, record): f=open('test.log', 'a'); f.write(str(record))`) ... and I see other log messages, but not the IOError. – EMiller Feb 22 '12 at 17:06
  • It would be helpful if you posted what you have tried in terms of the logging configuration and custom filter code. – Mark Lavin Feb 22 '12 at 18:07