@@ -356,14 +356,9 @@ def connect(self):
356
356
357
357
self ._sock .setblocking (False )
358
358
self .state = ConnectionStates .CONNECTING
359
- if self .config ['security_protocol' ] in ('SSL' , 'SASL_SSL' ):
360
- self ._wrap_ssl ()
361
- # _wrap_ssl can alter the connection state -- disconnects on failure
362
- # so we need to double check that we are still connecting before
363
- if self .connecting ():
364
- self .config ['state_change_callback' ](self )
365
- log .info ('%s: connecting to %s:%d [%s %s]' , self , self .host ,
366
- self .port , self ._sock_addr , AFI_NAMES [self ._sock_afi ])
359
+ self .config ['state_change_callback' ](self )
360
+ log .info ('%s: connecting to %s:%d [%s %s]' , self , self .host ,
361
+ self .port , self ._sock_addr , AFI_NAMES [self ._sock_afi ])
367
362
368
363
if self .state is ConnectionStates .CONNECTING :
369
364
# in non-blocking mode, use repeated calls to socket.connect_ex
@@ -373,29 +368,29 @@ def connect(self):
373
368
ret = self ._sock .connect_ex (self ._sock_addr )
374
369
except socket .error as err :
375
370
ret = err .errno
376
- except ValueError as err :
377
- # Python 3.7 and higher raises ValueError if a socket
378
- # is already connected
379
- if sys .version_info >= (3 , 7 ):
380
- ret = None
381
- else :
382
- raise
383
371
384
372
# Connection succeeded
385
373
if not ret or ret == errno .EISCONN :
386
374
log .debug ('%s: established TCP connection' , self )
375
+
387
376
if self .config ['security_protocol' ] in ('SSL' , 'SASL_SSL' ):
388
377
log .debug ('%s: initiating SSL handshake' , self )
389
378
self .state = ConnectionStates .HANDSHAKE
379
+ self .config ['state_change_callback' ](self )
380
+ # _wrap_ssl can alter the connection state -- disconnects on failure
381
+ self ._wrap_ssl ()
382
+
390
383
elif self .config ['security_protocol' ] == 'SASL_PLAINTEXT' :
391
384
log .debug ('%s: initiating SASL authentication' , self )
392
385
self .state = ConnectionStates .AUTHENTICATING
386
+ self .config ['state_change_callback' ](self )
387
+
393
388
else :
394
389
# security_protocol PLAINTEXT
395
390
log .info ('%s: Connection complete.' , self )
396
391
self .state = ConnectionStates .CONNECTED
397
392
self ._reset_reconnect_backoff ()
398
- self .config ['state_change_callback' ](self )
393
+ self .config ['state_change_callback' ](self )
399
394
400
395
# Connection failed
401
396
# WSAEINVAL == 10022, but errno.WSAEINVAL is not available on non-win systems
@@ -486,9 +481,6 @@ def _try_handshake(self):
486
481
# old ssl in python2.6 will swallow all SSLErrors here...
487
482
except (SSLWantReadError , SSLWantWriteError ):
488
483
pass
489
- # python 3.7 throws OSError
490
- except OSError :
491
- pass
492
484
except (SSLZeroReturnError , ConnectionError , SSLEOFError ):
493
485
log .warning ('SSL connection closed by server during handshake.' )
494
486
self .close (Errors .KafkaConnectionError ('SSL connection closed by server during handshake' ))
0 commit comments