from OpenSSL import SSL

def ssl_async_handshake(sock, hub, next):
    def shake():
        try:
            sock.do_handshake()
        except SSL.WantReadError:
            hub.disable_write(sock)
        except SSL.WantWriteError:
            hub.enable_write(sock)
        except SSL.WantX509LookupError:
            pass
        except SSL.ZeroReturnError:
            hub.unregister(sock) # and ignore
        except:
            hub.unregister(sock)
            raise
        else:
            hub.unregister(sock)
            next()
    hub.register(sock, shake, shake, shake)
    shake()

from OpenSSL import SSL

def ssl_async_handshake(sock, hub, next):
    def shake():
        try:
            sock.do_handshake()
        except SSL.WantReadError:
            hub.disable_write(sock)
        except SSL.WantWriteError:
            hub.enable_write(sock)
        except SSL.WantX509LookupError:
            pass
        except SSL.ZeroReturnError:
            hub.unregister(sock) # and ignore
        except SSL.SysCallError:
            hub.unregister(sock) # and ignore
        except:
            hub.unregister(sock)
            raise
        else:
            hub.unregister(sock)
            next()
    hub.register(sock, shake, shake, shake)
    shake()

handle syscall failure on handshake too