Web app hangs for several hours in ssl.py at self._sslobj.do_handshake() Web app hangs for several hours in ssl.py at self._sslobj.do_handshake() python python

Web app hangs for several hours in ssl.py at self._sslobj.do_handshake()


After browsing through the Python section of Stack Overflow, I found something which may not fix the core issue causing the problem, but is definitely good enough to handle any situation in which this issue pops up. The following question has various solutions which will throw some sort of exception if a function takes too long to complete. That is how I solved this issue in the end. The top answer is UNIX-only, however there are some others which use threading and work on every platform:

Timeout function if it takes too long to finish

This is a strange issue which is honestly pretty hard to reproduce. I've only seen it twice after thousands and thousands of API calls. I think it's pretty unlikely that someone will arrive at a better solution than this one, which is sort of a hack, but definitely solves the problem. You can throw an exception, then either attempt the SSL connection again, or continue with another part of your program.

I think my answer will suffice for now, but if someone has something better, feel free to offer it forward. Honestly, it seems like the only fix for the underlying issue might be a bug fix in the actual ssl.py library, but it's impossible for me to say for sure.


I ran into this and was able to work around it using socket.setdefaulttimeout. socket.setdefaulttimeout modifies the behavior of all sockets created after calling it, which is convenient if the socket is encapsulated in library code. If you have access to the specific SSL socket you can probably use socket.settimeout.