title: Flask broken pipe when redirecting after AJAX call
body: <p>I have an JQuery AJAX call:</p>
  
  <pre><code>$.getJSON($SCRIPT_ROOT + '/_click_btn?btnId='+$(this).attr("id"),
            $('form').serialize(),
            function(data) {
               // return to send_messages page
               window.location = 'send_messages';
  });
  </code></pre>
  
  <p>It doesn't do anything fancy. It just saves some form data to a database (using SQLAlchemy).  I put a break point on the window.location statement and the broken pipe won't happen if I delay the redirection by 1-2 seconds.  What's the best practice for handling this?</p>
  
  <p>One other item of note is the DB session remains open after the AJAX call is complete.</p>
  
  <p>The error message is below.</p>
  
  <pre><code>Exception in thread Thread-1:
  Traceback (most recent call last):
  File "/usr/lib64/python2.7/threading.py", line 813, in __bootstrap_inner self.run()
  File "/usr/lib64/python2.7/threading.py", line 766, in run self.__target(*self.__args, **self.__kwargs)
  File "/gui/venv/lib/python2.7/site-packages/werkzeug/serving.py", line 656, in inner srv.serve_forever()
  File "/gui/venv/lib/python2.7/site-packages/werkzeug/serving.py", line 496, in serve_forever HTTPServer.serve_forever(self)
  File "/usr/lib64/python2.7/SocketServer.py", line 238, in serve_forever
  self._handle_request_noblock()
  File "/usr/lib64/python2.7/SocketServer.py", line 297, in _handle_request_noblock
  self.handle_error(request, client_address)
  File "/usr/lib64/python2.7/SocketServer.py", line 295, in _handle_request_noblock
  self.process_request(request, client_address)
  File "/usr/lib64/python2.7/SocketServer.py", line 321, in process_request
  self.finish_request(request, client_address)
  File "/usr/lib64/python2.7/SocketServer.py", line 334, in finish_request
  self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib64/python2.7/SocketServer.py", line 655, in __init__
  self.handle()
  File "/gui/venv/lib/python2.7/site-packages/werkzeug/serving.py", line 216, in handle rv = BaseHTTPRequestHandler.handle(self)
  File "/usr/lib64/python2.7/BaseHTTPServer.py", line 340, in handle
  self.handle_one_request()
  File "/gui/venv/lib/python2.7/site-packages/werkzeug/serving.py", line 251, in handle_one_request
  return self.run_wsgi()
  File "/gui/venv/lib/python2.7/site-packages/werkzeug/serving.py", line 193, in run_wsgi
  execute(self.server.app)
  File "/gui/venv/lib/python2.7/site-packages/werkzeug/serving.py", line 184, in execute
  write(data)
  File "/gui/venv/lib/python2.7/site-packages/werkzeug/serving.py", line 152, in write
  self.send_header(key, value)
   File "/usr/lib64/python2.7/BaseHTTPServer.py", line 401, in send_header
  self.wfile.write("%s: %s\r\n" % (keyword, value))
  IOError: [Errno 32] Broken pipe
  </code></pre>