From fa2b9380c057ebd951b33f3a97938048a9aa2908 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Mon, 26 Apr 2010 22:17:47 +0000 Subject: [PATCH] Hopefully fix sporadic Windows issue by avoiding calling getpeername() on a freshly dup'ed socket. --- Lib/ssl.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/Lib/ssl.py b/Lib/ssl.py index 8ab32549955..ba3f09167fe 100644 --- a/Lib/ssl.py +++ b/Lib/ssl.py @@ -83,6 +83,7 @@ from socket import socket, AF_INET, SOCK_STREAM import base64 # for DER-to-PEM translation import traceback import errno +import time class SSLSocket(socket): @@ -97,6 +98,7 @@ class SSLSocket(socket): family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None, suppress_ragged_eofs=True, ciphers=None): + connected = False if sock is not None: socket.__init__(self, family=sock.family, @@ -104,26 +106,27 @@ class SSLSocket(socket): proto=sock.proto, fileno=_dup(sock.fileno())) self.settimeout(sock.gettimeout()) + # see if it's connected + try: + sock.getpeername() + except socket_error as e: + if e.errno != errno.ENOTCONN: + raise + else: + connected = True sock.close() elif fileno is not None: socket.__init__(self, fileno=fileno) else: socket.__init__(self, family=family, type=type, proto=proto) - self._closed = False - if certfile and not keyfile: keyfile = certfile - # see if it's connected - try: - socket.getpeername(self) - except socket_error as e: - if e.errno != errno.ENOTCONN: - raise - # no, no connection yet - self._sslobj = None - else: - # yes, create the SSL object + + self._closed = False + self._sslobj = None + if connected: + # create the SSL object try: self._sslobj = _ssl.sslwrap(self, server_side, keyfile, certfile,