Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Occasionally hangs in ssl do_handshake() #13

Open
ojensen5115 opened this issue Mar 7, 2015 · 4 comments · May be fixed by #110
Open

Occasionally hangs in ssl do_handshake() #13

ojensen5115 opened this issue Mar 7, 2015 · 4 comments · May be fixed by #110

Comments

@ojensen5115
Copy link
Contributor

I've been experiencing a lot of "hangs" recently, where the server simply stops responding to anything. I set my code to dump the stack on SIGINT I get the following:

File: "chat.py", line 856, in <module>
  server.serveforever()
File: "SimpleWebSocketServer/SimpleWebSocketServer.py", line 609, in serveforever
  super(SimpleSSLWebSocketServer, self).serveforever()
File: "SimpleWebSocketServer/SimpleWebSocketServer.py", line 530, in serveforever
  newsock = self.decorateSocket(sock)
File: "SimpleWebSocketServer/SimpleWebSocketServer.py", line 600, in decorateSocket
  ssl_version=self.version)
File: "/usr/lib/python2.7/ssl.py", line 381, in wrap_socket
  ciphers=ciphers)
File: "/usr/lib/python2.7/ssl.py", line 143, in __init__
  self.do_handshake()
File: "/usr/lib/python2.7/ssl.py", line 305, in do_handshake
  self._sslobj.do_handshake()
File: "chat.py", line 845, in close_sig_handler
  for filename, lineno, name, line in traceback.extract_stack(stack):

Looking around Google, it seems that a number of people (particularly on Debian and using OpenSSL, which is my setup) are experiencing occasional hangs in ssl.py's do_handshake() so this isn't really a SimpleWebSocketServer issue. However, the only solution I've found so far is to modify SimpleWebSocketServer.py to set a timeout on the decorateSocket() call, using something akin to what is suggested here: http://stackoverflow.com/a/2282656/146587

@dpallot
Copy link
Owner

dpallot commented Mar 9, 2015

Ok so it appears its taking too long to complete and not timing out. I'll look at a fix.

@dpallot
Copy link
Owner

dpallot commented Jul 20, 2015

Very hard to reproduce.

@ojensen5115
Copy link
Contributor Author

It is. I solved it in my system with the following:

timeout.py

from functools import wraps
import errno
import os
import signal

class TimeoutError(Exception):
    pass

def timeout(seconds=10, error_message=os.strerror(errno.ETIME)):
    def decorator(func):
        def _handle_timeout(signum, frame):
            raise TimeoutError(error_message)

        def wrapper(*args, **kwargs):
            signal.signal(signal.SIGALRM, _handle_timeout)
            signal.alarm(seconds)
            try:
                result = func(*args, **kwargs)
            finally:
                signal.alarm(0)
            return result

        return wraps(func)(wrapper)

    return decorator

and then throwing a "@timeout(5)" decorator on the decorateSocket function in SimpleWebServerSocket.py. From what I understand this isn't platform independent though.

@dpallot
Copy link
Owner

dpallot commented Jul 20, 2015

Ok glad you solved it for your system. I will have a look and see if this can be merged and if its a good idea. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants