Search code examples
pythonpython-2.7flaskpython-unittestbroken-pipe

Python/Flask Unittest, suppress Broken Pipe Error 32


When running unittests I often get error: [Errno 32] Broken pipe. It seems to be a harmless error that happens during testing, but I haven't been able to prevent or otherwise suppress it.

Some things that I have tried include changing SIGPIPE handling to SIG_DFL and running the app with threaded=True. If I were to try/except I don't know which code to wrap since this is in the context of unittesting.

I don't care about catching/preventing the error as much as just suppressing its output while all the other tests finish running. What else should I try?

Edit:

Here is some example code that often, but does not always result in the error:

  def test_register(self):                                                                              
    self.driver.get(self.get_server_url() + url_for(u'register'))                                       
    body = self.driver.find_element_by_id(u'body')

    username_input = body.find_element_by_id(u'username')                                               
    username_input.send_keys(self.USER1_DISPLAY_USERNAME)               
    privacy_policy = body.find_element_by_id(u'privacy_policy')                                         
    privacy_policy.click()
    #NOTE: not shown - more lines filling out form elements exactly like the above lines

    self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")                       
    register_button = body.find_element_by_id(u'onclick-register')                                      
    register_button.click()

And here is an example of the error message:

----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 44668)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 599, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 657, in __init__
    self.finish()
  File "/usr/lib/python2.7/SocketServer.py", line 716, in finish
    self.wfile.close()
  File "/usr/lib/python2.7/socket.py", line 283, in close
    self.flush()
  File "/usr/lib/python2.7/socket.py", line 307, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------

This seems to only happen with integration tests. I'm using Selenium and a LiveServerTestCase with a LIVESERVER_PORT port set to 8943. The mention of port 44668 in the error message looks suspicious.

My problem sounds exactly like Suppress print output in unittests but none of those solutions work.


Solution

  • I found a solution by redirecting stderr and stdout.

    In create_app() add the following:

    _stderr, _stdout = sys.stderr, sys.stdout                                                           
    null = open(os.devnull,'wb')                                                     
    sys.stdout = sys.stderr = null