@@ -794,14 +794,28 @@ cdef class Loop:
794
794
nr.query(addr, flags)
795
795
return fut
796
796
797
+ cdef _new_reader_future(self , sock):
798
+ def _on_cancel (fut ):
799
+ if fut.cancelled():
800
+ self ._remove_reader(sock)
801
+
802
+ fut = self ._new_future()
803
+ fut.add_done_callback(_on_cancel)
804
+ return fut
805
+
806
+ cdef _new_writer_future(self , sock):
807
+ def _on_cancel (fut ):
808
+ if fut.cancelled():
809
+ self ._remove_writer(sock)
810
+
811
+ fut = self ._new_future()
812
+ fut.add_done_callback(_on_cancel)
813
+ return fut
814
+
797
815
cdef _sock_recv(self , fut, sock, n):
798
816
cdef:
799
817
Handle handle
800
818
801
- if fut.cancelled():
802
- self ._remove_reader(sock)
803
- return
804
-
805
819
try :
806
820
data = sock.recv(n)
807
821
except (BlockingIOError, InterruptedError):
@@ -819,10 +833,6 @@ cdef class Loop:
819
833
cdef:
820
834
Handle handle
821
835
822
- if fut.cancelled():
823
- self ._remove_reader(sock)
824
- return
825
-
826
836
try :
827
837
data = sock.recv_into(buf)
828
838
except (BlockingIOError, InterruptedError):
@@ -841,10 +851,6 @@ cdef class Loop:
841
851
Handle handle
842
852
int n
843
853
844
- if fut.cancelled():
845
- self ._remove_writer(sock)
846
- return
847
-
848
854
try :
849
855
n = sock.send(data)
850
856
except (BlockingIOError, InterruptedError):
@@ -878,10 +884,6 @@ cdef class Loop:
878
884
cdef:
879
885
Handle handle
880
886
881
- if fut.cancelled():
882
- self ._remove_reader(sock)
883
- return
884
-
885
887
try :
886
888
conn, address = sock.accept()
887
889
conn.setblocking(False )
@@ -896,31 +898,29 @@ cdef class Loop:
896
898
fut.set_result((conn, address))
897
899
self ._remove_reader(sock)
898
900
899
- cdef _sock_connect(self , fut, sock, address):
901
+ cdef _sock_connect(self , sock, address):
900
902
cdef:
901
903
Handle handle
902
904
903
905
try :
904
906
sock.connect(address)
905
907
except (BlockingIOError, InterruptedError):
906
- # Issue #23618: When the C function connect() fails with EINTR, the
907
- # connection runs in background. We have to wait until the socket
908
- # becomes writable to be notified when the connection succeed or
909
- # fails.
910
- fut.add_done_callback(lambda fut : self ._remove_writer(sock))
908
+ pass
909
+ else :
910
+ return
911
911
912
- handle = new_MethodHandle3(
913
- self ,
914
- " Loop._sock_connect" ,
915
- < method3_t> self ._sock_connect_cb,
916
- self ,
917
- fut, sock, address)
912
+ fut = self ._new_future()
913
+ fut.add_done_callback(lambda fut : self ._remove_writer(sock))
918
914
919
- self ._add_writer(sock, handle)
920
- except Exception as exc:
921
- fut.set_exception(exc)
922
- else :
923
- fut.set_result(None )
915
+ handle = new_MethodHandle3(
916
+ self ,
917
+ " Loop._sock_connect" ,
918
+ < method3_t> self ._sock_connect_cb,
919
+ self ,
920
+ fut, sock, address)
921
+
922
+ self ._add_writer(sock, handle)
923
+ return fut
924
924
925
925
cdef _sock_connect_cb(self , fut, sock, address):
926
926
if fut.cancelled():
@@ -2087,7 +2087,7 @@ cdef class Loop:
2087
2087
if self ._debug and sock.gettimeout() != 0 :
2088
2088
raise ValueError (" the socket must be non-blocking" )
2089
2089
2090
- fut = self ._new_future( )
2090
+ fut = self ._new_reader_future(sock )
2091
2091
handle = new_MethodHandle3(
2092
2092
self ,
2093
2093
" Loop._sock_recv" ,
@@ -2112,7 +2112,7 @@ cdef class Loop:
2112
2112
if self ._debug and sock.gettimeout() != 0 :
2113
2113
raise ValueError (" the socket must be non-blocking" )
2114
2114
2115
- fut = self ._new_future( )
2115
+ fut = self ._new_reader_future(sock )
2116
2116
handle = new_MethodHandle3(
2117
2117
self ,
2118
2118
" Loop._sock_recv_into" ,
@@ -2162,7 +2162,7 @@ cdef class Loop:
2162
2162
data = memoryview(data)
2163
2163
data = data[n:]
2164
2164
2165
- fut = self ._new_future( )
2165
+ fut = self ._new_writer_future(sock )
2166
2166
handle = new_MethodHandle3(
2167
2167
self ,
2168
2168
" Loop._sock_sendall" ,
@@ -2191,7 +2191,7 @@ cdef class Loop:
2191
2191
if self ._debug and sock.gettimeout() != 0 :
2192
2192
raise ValueError (" the socket must be non-blocking" )
2193
2193
2194
- fut = self ._new_future( )
2194
+ fut = self ._new_reader_future(sock )
2195
2195
handle = new_MethodHandle2(
2196
2196
self ,
2197
2197
" Loop._sock_accept" ,
@@ -2212,15 +2212,13 @@ cdef class Loop:
2212
2212
2213
2213
socket_inc_io_ref(sock)
2214
2214
try :
2215
- fut = self ._new_future()
2216
2215
if sock.family == uv.AF_UNIX:
2217
- self ._sock_connect(fut, sock, address)
2216
+ fut = self ._sock_connect(sock, address)
2217
+ else :
2218
+ _, _, _, _, address = (await self .getaddrinfo(* address[:2 ]))[0 ]
2219
+ fut = self ._sock_connect(sock, address)
2220
+ if fut is not None :
2218
2221
await fut
2219
- return
2220
-
2221
- _, _, _, _, address = (await self .getaddrinfo(* address[:2 ]))[0 ]
2222
- self ._sock_connect(fut, sock, address)
2223
- await fut
2224
2222
finally :
2225
2223
socket_dec_io_ref(sock)
2226
2224
0 commit comments