@@ -1016,38 +1016,43 @@ async def _connect_sock(self, exceptions, addr_info, local_addr_infos=None):
10161016 family , type_ , proto , _ , address = addr_info
10171017 sock = None
10181018 try :
1019- sock = socket .socket (family = family , type = type_ , proto = proto )
1020- sock .setblocking (False )
1021- if local_addr_infos is not None :
1022- for lfamily , _ , _ , _ , laddr in local_addr_infos :
1023- # skip local addresses of different family
1024- if lfamily != family :
1025- continue
1026- try :
1027- sock .bind (laddr )
1028- break
1029- except OSError as exc :
1030- msg = (
1031- f'error while attempting to bind on '
1032- f'address { laddr !r} : { str (exc ).lower ()} '
1033- )
1034- exc = OSError (exc .errno , msg )
1035- my_exceptions .append (exc )
1036- else : # all bind attempts failed
1037- if my_exceptions :
1038- raise my_exceptions .pop ()
1039- else :
1040- raise OSError (f"no matching local address with { family = } found" )
1041- await self .sock_connect (sock , address )
1042- return sock
1043- except OSError as exc :
1044- my_exceptions .append (exc )
1045- if sock is not None :
1046- sock .close ()
1047- raise
1019+ try :
1020+ sock = socket .socket (family = family , type = type_ , proto = proto )
1021+ sock .setblocking (False )
1022+ if local_addr_infos is not None :
1023+ for lfamily , _ , _ , _ , laddr in local_addr_infos :
1024+ # skip local addresses of different family
1025+ if lfamily != family :
1026+ continue
1027+ try :
1028+ sock .bind (laddr )
1029+ break
1030+ except OSError as exc :
1031+ msg = (
1032+ f'error while attempting to bind on '
1033+ f'address { laddr !r} : { str (exc ).lower ()} '
1034+ )
1035+ exc = OSError (exc .errno , msg )
1036+ my_exceptions .append (exc )
1037+ else : # all bind attempts failed
1038+ if my_exceptions :
1039+ raise my_exceptions .pop ()
1040+ else :
1041+ raise OSError (f"no matching local address with { family = } found" )
1042+ await self .sock_connect (sock , address )
1043+ return sock
1044+ except OSError as exc :
1045+ my_exceptions .append (exc )
1046+ raise
10481047 except :
10491048 if sock is not None :
1050- sock .close ()
1049+ try :
1050+ sock .close ()
1051+ except OSError :
1052+ # An error when closing a newly created socket is
1053+ # not important, but it can overwrite more important
1054+ # non-OSError error. So ignore it.
1055+ pass
10511056 raise
10521057 finally :
10531058 exceptions = my_exceptions = None
0 commit comments