@@ -240,20 +240,20 @@ async def _connect_secure(cls, resolved_address, timeout, keep_alive, ssl):
240240 log .debug ("[#0000] S: <TIMEOUT> %s" , resolved_address )
241241 log .debug ("[#0000] C: <CLOSE> %s" , resolved_address )
242242 if s :
243- await cls .close_socket (s )
243+ cls ._kill_raw_socket (s )
244244 raise ServiceUnavailable (
245245 "Timed out trying to establish connection to {!r}" .format (
246246 resolved_address )) from None
247247 except asyncio .CancelledError :
248248 log .debug ("[#0000] S: <CANCELLED> %s" , resolved_address )
249249 log .debug ("[#0000] C: <CLOSE> %s" , resolved_address )
250250 if s :
251- await cls .close_socket (s )
251+ cls ._kill_raw_socket (s )
252252 raise
253253 except (SSLError , CertificateError ) as error :
254254 local_port = s .getsockname ()[1 ]
255255 if s :
256- await cls .close_socket (s )
256+ cls ._kill_raw_socket (s )
257257 raise BoltSecurityError (
258258 message = "Failed to establish encrypted connection." ,
259259 address = (resolved_address ._host_name , local_port )
@@ -263,7 +263,7 @@ async def _connect_secure(cls, resolved_address, timeout, keep_alive, ssl):
263263 " " .join (map (repr , error .args )))
264264 log .debug ("[#0000] C: <CLOSE> %s" , resolved_address )
265265 if s :
266- await cls .close_socket (s )
266+ cls ._kill_raw_socket (s )
267267 if isinstance (error , OSError ):
268268 raise ServiceUnavailable (
269269 "Failed to establish connection to {!r} (reason {})"
@@ -350,14 +350,18 @@ async def close_socket(cls, socket_):
350350 except OSError :
351351 pass
352352 else :
353- try :
354- socket_ .shutdown (SHUT_RDWR )
355- except OSError :
356- pass
357- try :
358- socket_ .close ()
359- except OSError :
360- pass
353+ cls ._kill_raw_socket (socket_ )
354+
355+ @classmethod
356+ def _kill_raw_socket (cls , socket_ ):
357+ try :
358+ socket_ .shutdown (SHUT_RDWR )
359+ except OSError :
360+ pass
361+ try :
362+ socket_ .close ()
363+ except OSError :
364+ pass
361365
362366 @classmethod
363367 async def connect (cls , address , * , tcp_timeout , deadline ,
@@ -409,7 +413,10 @@ async def connect(cls, address, *, tcp_timeout, deadline,
409413 log .debug ("[#%04X] C: <CANCELED> %s" , local_port ,
410414 resolved_address )
411415 if s :
412- await cls .close_socket (s )
416+ try :
417+ s .kill ()
418+ except OSError :
419+ pass
413420 raise
414421 except Exception :
415422 if s :
@@ -528,15 +535,15 @@ def _connect(cls, resolved_address, timeout, keep_alive):
528535 except SocketTimeout :
529536 log .debug ("[#0000] S: <TIMEOUT> %s" , resolved_address )
530537 log .debug ("[#0000] C: <CLOSE> %s" , resolved_address )
531- cls .close_socket (s )
538+ cls ._kill_raw_socket (s )
532539 raise ServiceUnavailable (
533540 "Timed out trying to establish connection to {!r}" .format (
534541 resolved_address ))
535542 except Exception as error :
536543 log .debug ("[#0000] S: <ERROR> %s %s" , type (error ).__name__ ,
537544 " " .join (map (repr , error .args )))
538545 log .debug ("[#0000] C: <CLOSE> %s" , resolved_address )
539- cls .close_socket (s )
546+ cls ._kill_raw_socket (s )
540547 if isinstance (error , OSError ):
541548 raise ServiceUnavailable (
542549 "Failed to establish connection to {!r} (reason {})"
@@ -554,7 +561,7 @@ def _secure(cls, s, host, ssl_context):
554561 sni_host = host if HAS_SNI and host else None
555562 s = ssl_context .wrap_socket (s , server_hostname = sni_host )
556563 except (OSError , SSLError , CertificateError ) as cause :
557- cls .close_socket (s )
564+ cls ._kill_raw_socket (s )
558565 raise BoltSecurityError (
559566 message = "Failed to establish encrypted connection." ,
560567 address = (host , local_port )
@@ -615,15 +622,15 @@ def _handshake(cls, s, resolved_address, deadline):
615622 # If no data is returned after a successful select
616623 # response, the server has closed the connection
617624 log .debug ("[#%04X] S: <CLOSE>" , local_port )
618- cls .close_socket (s )
625+ cls ._kill_raw_socket (s )
619626 raise ServiceUnavailable (
620627 f"Connection to { resolved_address } closed without handshake "
621628 "response"
622629 )
623630 if data_size != 4 :
624631 # Some garbled data has been received
625632 log .debug ("[#%04X] S: @*#!" , local_port )
626- cls .close_socket (s )
633+ cls ._kill_raw_socket (s )
627634 raise BoltProtocolError (
628635 "Expected four byte Bolt handshake response from "
629636 f"{ resolved_address !r} , received { response !r} instead; "
@@ -632,7 +639,7 @@ def _handshake(cls, s, resolved_address, deadline):
632639 )
633640 elif response == b"HTTP" :
634641 log .debug ("[#%04X] S: <CLOSE>" , local_port )
635- cls .close_socket (s )
642+ cls ._kill_raw_socket (s )
636643 raise ServiceUnavailable (
637644 f"Cannot to connect to Bolt service on { resolved_address !r} "
638645 "(looks like HTTP)"
@@ -646,6 +653,10 @@ def _handshake(cls, s, resolved_address, deadline):
646653 def close_socket (cls , socket_ ):
647654 if isinstance (socket_ , BoltSocket ):
648655 socket_ = socket_ ._socket
656+ cls ._kill_raw_socket (socket_ )
657+
658+ @classmethod
659+ def _kill_raw_socket (cls , socket_ ):
649660 try :
650661 socket_ .shutdown (SHUT_RDWR )
651662 except OSError :
0 commit comments