@@ -101,7 +101,7 @@ def __init__(self, host, port,
101
101
'''
102
102
if os .name == 'nt' :
103
103
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
105
105
)
106
106
else :
107
107
libc = ctypes .CDLL (ctypes .util .find_library ('c' ), use_errno = True )
@@ -120,6 +120,7 @@ def __init__(self, host, port,
120
120
self .schema_version = 1
121
121
self ._socket = None
122
122
self .connected = False
123
+ self .inconnect = False
123
124
self .error = True
124
125
self .encoding = encoding
125
126
self .call_16 = call_16
@@ -193,6 +194,7 @@ def connect(self):
193
194
:raise: `NetworkError`
194
195
'''
195
196
try :
197
+ self .inconnect = True
196
198
self .connect_basic ()
197
199
self .handshake ()
198
200
# It is important to set socket timeout *after* connection.
@@ -201,8 +203,10 @@ def connect(self):
201
203
# not bound to port
202
204
self ._socket .settimeout (self .socket_timeout )
203
205
self .load_schema ()
206
+ self .inconnect = False
204
207
except socket .error as e :
205
208
self .connected = False
209
+ self .inconnect = False
206
210
raise NetworkError (e )
207
211
208
212
def _recv (self , to_read ):
@@ -271,6 +275,8 @@ def _opt_reconnect(self):
271
275
Check that connection is alive using low-level recv from libc(ctypes)
272
276
**Due to bug in python - timeout is internal python construction.
273
277
'''
278
+ if self .inconnect :
279
+ return
274
280
if not self ._socket :
275
281
return self .connect ()
276
282
@@ -287,13 +293,19 @@ def check(): # Check that connection is alive
287
293
self ._socket .setblocking (False )
288
294
else :
289
295
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 )
291
297
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 ):
293
305
ctypes .set_errno (0 )
294
306
return errno .EAGAIN
295
- return ( ctypes . get_errno () if ctypes . get_errno ()
296
- else errno .ECONNRESET )
307
+ else :
308
+ return errno .ECONNRESET
297
309
298
310
last_errno = check ()
299
311
if self .connected and last_errno == errno .EAGAIN :
0 commit comments