I have been running a simple flask webserver for the purpose of a twilio app. Usually works all groovy, however every few days, twilio will tell me it could not connect, and upon checking the running server python shell, it would show nothing abnormal until I killed it, and the stack trace would spit out an exception that my flask app had crashed related to the generic twilio request.
Googling around, it would appear this is due to an underlying SocketServer not handling the exception properly and causing the problem. This post on stackoverflow speaks about the issue but offers no solution: http://stackoverflow.com/questions/17787023/python-how-to-catch-a-flask-except-like-this
It appears from the leading comment, that it's 'expected' to happen, and flask should simply handle it fine. However, in my case, it causes the server to lock up and crash. No further requests will be acknowledged. I simply have to re-start the app, and it'll proceed merrily along for a few more days.
If this is truly a bug in flask, it makes relatively easy to DDoS any flask server by the client simply hitting the web request and then stopping quickly and not acknowledging.
Why is my flask not set up to properly handle this type of thing? Is this a bug? Have I improperly configured something? Is there a try/catch I'm forgetting?
using regular pip install of flask 0.10.1 btw... The exception is pasted below:
Exception happened during processing of request from ('x.x.x.x', 48590)
Traceback (most recent call last):
File "/root/.pyenv/versions/2.7.10/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/root/.pyenv/versions/2.7.10/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/root/.pyenv/versions/2.7.10/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/root/.pyenv/versions/2.7.10/lib/python2.7/SocketServer.py", line 655, in __init__
self.handle()
File "/root/.pyenv/versions/2.7.10/lib/python2.7/site-packages/werkzeug/serving.py", line 203, in handle
rv = BaseHTTPRequestHandler.handle(self)
File "/root/.pyenv/versions/2.7.10/lib/python2.7/BaseHTTPServer.py", line 340, in handle
self.handle_one_request()
File "/root/.pyenv/versions/2.7.10/lib/python2.7/site-packages/werkzeug/serving.py", line 234, in handle_one_request
self.raw_requestline = self.rfile.readline()
File "/root/.pyenv/versions/2.7.10/lib/python2.7/socket.py", line 451, in readline
data = self._sock.recv(self._rbufsize)
KeyboardInterrupt
The dev server you are using is not suitable for production. See http://flask.pocoo.org/docs/0.10/deploying/ for ways on how to deploy it using a proper webserver.
for deployment please use a production grade wsgi server - the devserver was never intended to be used on a deployed production system
Wow, pardon my initial ignorance on this! I was under the impression flask could be used "out of the box" in production...
Thanks everyone for the tips! I've wrapped my app in Tornado, so we'll see how it goes... so far, so good...
Tornado's own docs tell you not to run a WSGI app with it unless you have a good reason. Consider uWSGI or Gunicorn instead.
@actioncrypto flask indeed can be used out of the box, however as wsgi framework, not as appserver
i think the distinction is important to keep in mind
Yes, this distinction would certainly be very important to note, thank you kindly... I was led to believe flask would be sufficient for twilio server -- from one of twilio's own quickstarts: here
Is this incorrect, and should twilio be notified to change their help docs?
Well for a 'quickstart' like they describe it makes sense to use the dev server. But I guess it would indeed make sense for them to mention that it's not suitable for production...
@ThiefMaster This is mentioned in the unstable docs.
Most helpful comment
Wow, pardon my initial ignorance on this! I was under the impression flask could be used "out of the box" in production...
Thanks everyone for the tips! I've wrapped my app in Tornado, so we'll see how it goes... so far, so good...