Skip to content

Commit 378ba17

Browse files
authored
Fixed AssertionError and unnecessary connect delay (nedbat#94)
If an earlier connection attempt succeeds, it should signal that the other attempts should be cancelled. This patch makes the first successful attempt cancel the entire scope immediately to achieve that effect.
1 parent 473f940 commit 378ba17

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

anyio/__init__.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,12 @@ async def try_connect(af: int, addr: str, event: Event):
347347
await sock.close()
348348
raise
349349
else:
350-
assert stream is None
351-
stream = _networking.SocketStream(sock, ssl_context, target_host,
352-
tls_standard_compatible)
350+
if stream is None:
351+
stream = _networking.SocketStream(sock, ssl_context, target_host,
352+
tls_standard_compatible)
353+
await tg.cancel_scope.cancel()
354+
else:
355+
raw_socket.close()
353356
finally:
354357
await event.set()
355358

@@ -393,10 +396,6 @@ async def try_connect(af: int, addr: str, event: Event):
393396
async with move_on_after(happy_eyeballs_delay):
394397
await event.wait()
395398

396-
if stream is not None:
397-
await tg.cancel_scope.cancel()
398-
break
399-
400399
if stream is None:
401400
cause = oserrors[0] if len(oserrors) == 1 else asynclib.ExceptionGroup(oserrors)
402401
raise OSError('All connection attempts failed') from cause

0 commit comments

Comments
 (0)