Hi all,

I am using Twisted Web WSGIResource to host a Django site. Here's the code I use to setup the WSGI resource (other boilerplate skipped)

def wsgiresource():
from django.core.handlers.wsgi import WSGIHandler
pool = threadpool.ThreadPool()
pool.start()
# Allow Ctrl-C to get you out cleanly:
reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
wsgi
resource = wsgi.WSGIResource(reactor, pool, WSGIHandler())
return wsgi_resource

This generally worked quite well. However I have an API endpoint that gets hit with multiple POST requests per second (not very much - I'd say 10-20 req/sec at most). Sometimes I see this in the logs:

Internal Server Error: /cas/eventlog/
Traceback (most recent call last):
File "/site-packages/django/core/handlers/base.py", line 114, in getresponse
response = wrapped
callback(request, callback_args, *callbackkwargs)
File "/site-packages/django/views/decorators/csrf.py", line 57, in wrapped
view
return viewfunc(args, *kwargs)
File "/views.py", line 488, in event
log
data = json.loads(request.body)
File "/site-packages/django/http/request.py", line 192, in body
self.body = self.read()
File "/site-packages/django/http/request.py", line 246, in read
return self.
stream.read(args, *kwargs)
File "/site-packages/django/core/handlers/wsgi.py", line 45, in read
result = self.buffer + self.readlimited()
File "/site-packages/django/core/handlers/wsgi.py", line 39, in readlimited
result = self.stream.read(size)
File "/site-packages/twisted/web/wsgi.py", line 94, in read
return self._wrapped.read(size)
ValueError: I/O operation on closed file.

Any pointers on why would this happen? Why would Twisted close the file early? Could it be that I need to increase the threadpool size?

I use treq 0.2.0 for the requests, with the "persistent" flag turned on, if that makes any difference. Twisted 13.2.0 and Django 1.6, python 2.7.1 on OS X 10.7.5.

Many thanks,
Orestis