title: How to catch "[Errno 32] Broken pipe" in a WSGI handler
body: <p>WSGI is extremely useful for building highly concurrent HTTP servers to support e.g. long polling, however, typically, the long running HTTP request will at some point be ended by the client side; to clean up any resources and open handles, the WSGI server backend should be notified of any such events, however, it doesn't currently seem to be possible to catch those events in the WSGI handler:</p>
  
  <pre><code># pseudocode example
  
  def application(env, start_response):
      start_response(...)
  
      q = Queue()
      ev_handle = register_event_handler(lambda event, arg: q.put((event, arg)))
  
      # ??? need to call e.g. ev_handle.unregister() when the HTTP request is terminated
  
      return iter(lambda: render(q.get()), None)
  </code></pre>
  
  <p>For example, when using <code>gevent.pywsgi</code>, the corresponding exception (<code>error: [Errno 32] Broken pipe</code>) is thrown somewhere inside gevent and never even seems to surface anywhere the handler could potentially see it:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/Users/erik.allik/.virtualenvs/myproj/lib/python2.7/site-packages/gevent/pywsgi.py", line 508, in handle_one_response
      self.run_application()
    File "/Users/erik.allik/.virtualenvs/myproj/lib/python2.7/site-packages/gevent/pywsgi.py", line 495, in run_application
      self.process_result()
    File "/Users/erik.allik/.virtualenvs/myproj/lib/python2.7/site-packages/gevent/pywsgi.py", line 486, in process_result
      self.write(data)
    File "/Users/erik.allik/.virtualenvs/myproj/lib/python2.7/site-packages/gevent/pywsgi.py", line 376, in write
      self._write(data)
    File "/Users/erik.allik/.virtualenvs/myproj/lib/python2.7/site-packages/gevent/pywsgi.py", line 369, in _write
      self._sendall(data)
    File "/Users/erik.allik/.virtualenvs/myproj/lib/python2.7/site-packages/gevent/pywsgi.py", line 355, in _sendall
      self.socket.sendall(data)
    File "/Users/erik.allik/.virtualenvs/myproj/lib/python2.7/site-packages/gevent/socket.py", line 458, in sendall
      data_sent += self.send(_get_memory(data, data_sent), flags)
    File "/Users/erik.allik/.virtualenvs/myproj/lib/python2.7/site-packages/gevent/socket.py", line 435, in send
      return sock.send(data, flags)
  </code></pre>