@@ -101,7 +101,7 @@ def __init__(self, host, port,
101101 '''
102102 if os .name == 'nt' :
103103 libc = ctypes .windll .LoadLibrary (
104- ctypes .util .find_library ('Ws2_32' ), use_errno = True
104+ ctypes .util .find_library ('Ws2_32' ), use_last_error = True
105105 )
106106 else :
107107 libc = ctypes .CDLL (ctypes .util .find_library ('c' ), use_errno = True )
@@ -120,6 +120,7 @@ def __init__(self, host, port,
120120 self .schema_version = 1
121121 self ._socket = None
122122 self .connected = False
123+ self .inconnect = False
123124 self .error = True
124125 self .encoding = encoding
125126 self .call_16 = call_16
@@ -193,6 +194,7 @@ def connect(self):
193194 :raise: `NetworkError`
194195 '''
195196 try :
197+ self .inconnect = True
196198 self .connect_basic ()
197199 self .handshake ()
198200 # It is important to set socket timeout *after* connection.
@@ -201,8 +203,10 @@ def connect(self):
201203 # not bound to port
202204 self ._socket .settimeout (self .socket_timeout )
203205 self .load_schema ()
206+ self .inconnect = False
204207 except socket .error as e :
205208 self .connected = False
209+ self .inconnect = False
206210 raise NetworkError (e )
207211
208212 def _recv (self , to_read ):
@@ -271,6 +275,8 @@ def _opt_reconnect(self):
271275 Check that connection is alive using low-level recv from libc(ctypes)
272276 **Due to bug in python - timeout is internal python construction.
273277 '''
278+ if self .inconnect :
279+ return
274280 if not self ._socket :
275281 return self .connect ()
276282
@@ -287,13 +293,19 @@ def check(): # Check that connection is alive
287293 self ._socket .setblocking (False )
288294 else :
289295 flag = socket .MSG_DONTWAIT | socket .MSG_PEEK
290- self ._sys_recv (sock_fd , buf , 1 , flag )
296+ retbytes = self ._sys_recv (sock_fd , buf , 1 , flag )
291297
292- if ctypes .get_errno () == errno .EAGAIN :
298+ err = 0
299+ if os .name != 'nt' :
300+ err = ctypes .get_errno ()
301+ else :
302+ err = ctypes .get_last_error ()
303+
304+ if (retbytes < 0 ) and (err == errno .EAGAIN or err == errno .EWOULDBLOCK ):
293305 ctypes .set_errno (0 )
294306 return errno .EAGAIN
295- return ( ctypes . get_errno () if ctypes . get_errno ()
296- else errno .ECONNRESET )
307+ else :
308+ return errno .ECONNRESET
297309
298310 last_errno = check ()
299311 if self .connected and last_errno == errno .EAGAIN :
0 commit comments