chevron-up bell reply instagram twitter2 feed3 finder search-25px-p0

Flask WSGI server error: code 400, message Bad request version (‘\xefg#\x00&\xc0,\xc0+\xc0$\xc0#\xc0’)

2017-06-13

When I went to office this morning, I was told one of company's website is not running properly. To be more specific, the whole web will be frozen every time 10 mins after a restart. The web was developed by myself and it was using Flask, with a SSL certificate in it.

As a first glance of the log:

10.10.10.10 - - [12/Jun/2017 14:30:26] code 400, message Bad request version ('\xefg#\x00&\xc0,\xc0+\xc0$\xc0#\xc0')
10.10.10.10 - - [12/Jun/2017 14:30:26] "^V^C^A^B^@^A^@^A?^C^C$E???GiVCE???d^]?9????<1d>?l?????^C N^??^U?ǛAf?E1^???^EV{c?^HW?A.?T^N^K?g#^@&?,?+?$?#?" 400 -
Traceback (most recent call last):
  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 651, in __init__
    self.finish()
  File "/usr/lib64/python2.7/SocketServer.py", line 710, in finish
    self.wfile.close()
  File "/usr/lib64/python2.7/socket.py", line 279, in close
    self.flush()
  File "/usr/lib64/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
  File "/usr/lib64/python2.7/ssl.py", line 750, in sendall
    return socket.sendall(self, data, flags)
  File "/usr/lib64/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)

I thought it might be a SSL certificate issue, which might not be configured correctly. As I simply deploy the code from git server, and not regenerate the SSL keys. After redo the key, the web is still the same. This time, I found a lot of TCP connections "Close Wait":

As we known, when a TCP connection is in "Close Wait" status, which means this connection is not fully released, and waiting for a close by the web server. So I go to the Flask server setting file.

manager.add_command("runserver_prod",
                    Server(host='0.0.0.0', port=8889, use_debugger=False, ssl_context=context_prod))

Wait, a web server must be multi-threading, instead of waiting for a single thread to do all the jobs, so when I add threaded=True and restart the web server, the issue disappeared.

manager.add_command("runserver_prod",
                    Server(host='0.0.0.0', port=8889, threaded=True, use_debugger=False, ssl_context=context_prod))

arkilis

Make Your Comments