Debian Codegraph @

src/trio/_highlevel_open_tcp_stream.py

raw
1 from __future__ import annotations
2
3 import sys
4 from contextlib import contextmanager, suppress
5 from typing import TYPE_CHECKING, Any
6
7 import trio
8 from trio.socket import SOCK_STREAM, SocketType, getaddrinfo, socket
9
10 if TYPE_CHECKING:
11 from collections.abc import Generator, MutableSequence
12 from socket import AddressFamily, SocketKind
13
14 from trio._socket import AddressFormat
15
16 if sys.version_info < (3, 11):
17 from exceptiongroup import BaseExceptionGroup, ExceptionGroup
18
19
20 # Implementation of RFC 6555 "Happy eyeballs"
21 # https://tools.ietf.org/html/rfc6555
22 #
23 # Basically, the problem here is that if we want to connect to some host, and
24 # DNS returns multiple IP addresses, then we don't know which of them will
25 # actually work -- it can happen that some of them are reachable, and some of
26 # them are not. One particularly common situation where this happens is on a
27 # host that thinks it has ipv6 connectivity, but really doesn't. But in
28 # principle this could happen for any kind of multi-home situation (e.g. the
29 # route to one mirror is down but another is up).
30 #
31 # The naive algorithm (e.g. the stdlib's socket.create_connection) would be to
32 # pick one of the IP addresses and try to connect; if that fails, try the
33 # next; etc. The problem with this is that TCP is stubborn, and if the first
34 # address is a blackhole then it might take a very long time (tens of seconds)
35 # before that connection attempt fails.
36 #
37 # That's where RFC 6555 comes in. It tells us that what we do is:
38 # - get the list of IPs from getaddrinfo, trusting the order it gives us (with
39 # one exception noted in section 5.4)
40 # - start a connection attempt to the first IP
41 # - when this fails OR if it's still going after DELAY seconds, then start a
42 # connection attempt to the second IP
43 # - when this fails OR if it's still going after another DELAY seconds, then
44 # start a connection attempt to the third IP
45 # - ... repeat until we run out of IPs.
46 #
47 # Our implementation is similarly straightforward: we spawn a chain of tasks,
48 # where each one (a) waits until the previous connection has failed or DELAY
49 # seconds have passed, (b) spawns the next task, (c) attempts to connect. As
50 # soon as any task crashes or succeeds, we cancel all the tasks and return.
51 #
52 # Note: this currently doesn't attempt to cache any results, so if you make
53 # multiple connections to the same host it'll re-run the happy-eyeballs
54 # algorithm each time. RFC 6555 is pretty confusing about whether this is
55 # allowed. Section 4 describes an algorithm that attempts ipv4 and ipv6
56 # simultaneously, and then says "The client MUST cache information regarding
57 # the outcome of each connection attempt, and it uses that information to
58 # avoid thrashing the network with subsequent attempts." Then section 4.2 says
59 # "implementations MUST prefer the first IP address family returned by the
60 # host's address preference policy, unless implementing a stateful
61 # algorithm". Here "stateful" means "one that caches information about
62 # previous attempts". So my reading of this is that IF you're starting ipv4
63 # and ipv6 at the same time then you MUST cache the result for ~ten minutes,
64 # but IF you're "preferring" one protocol by trying it first (like we are),
65 # then you don't need to cache.
66 #
67 # Caching is quite tricky: to get it right you need to do things like detect
68 # when the network interfaces are reconfigured, and if you get it wrong then
69 # connection attempts basically just don't work. So we don't even try.
70
71 # "Firefox and Chrome use 300 ms"
72 # https://tools.ietf.org/html/rfc6555#section-6
73 # Though
74 # https://www.researchgate.net/profile/Vaibhav_Bajpai3/publication/304568993_Measuring_the_Effects_of_Happy_Eyeballs/links/5773848e08ae6f328f6c284c/Measuring-the-Effects-of-Happy-Eyeballs.pdf
75 # claims that Firefox actually uses 0 ms, unless an about:config option is
76 # toggled and then it uses 250 ms.
77 DEFAULT_DELAY = 0.250
78
79 # How should we call getaddrinfo? In particular, should we use AI_ADDRCONFIG?
80 #
81 # The idea of AI_ADDRCONFIG is that it only returns addresses that might
82 # work. E.g., if getaddrinfo knows that you don't have any IPv6 connectivity,
83 # then it doesn't return any IPv6 addresses. And this is kinda nice, because
84 # it means maybe you can skip sending AAAA requests entirely. But in practice,
85 # it doesn't really work right.
86 #
87 # - on Linux/glibc, empirically, the default is to return all addresses, and
88 # with AI_ADDRCONFIG then it only returns IPv6 addresses if there is at least
89 # one non-loopback IPv6 address configured... but this can be a link-local
90 # address, so in practice I guess this is basically always configured if IPv6
91 # is enabled at all. OTOH if you pass in "::1" as the target address with
92 # AI_ADDRCONFIG and there's no *external* IPv6 address configured, you get an
93 # error. So AI_ADDRCONFIG mostly doesn't do anything, even when you would want
94 # it to, and when it does do something it might break things that would have
95 # worked.
96 #
97 # - on Windows 10, empirically, if no IPv6 address is configured then by
98 # default they are also suppressed from getaddrinfo (flags=0 and
99 # flags=AI_ADDRCONFIG seem to do the same thing). If you pass AI_ALL, then you
100 # get the full list.
101 # ...except for localhost! getaddrinfo("localhost", "80") gives me ::1, even
102 # though there's no ipv6 and other queries only return ipv4.
103 # If you pass in and IPv6 IP address as the target address, then that's always
104 # returned OK, even with AI_ADDRCONFIG set and no IPv6 configured.
105 #
106 # But I guess other versions of windows messed this up, judging from these bug
107 # reports:
108 # https://bugs.chromium.org/p/chromium/issues/detail?id=5234
109 # https://bugs.chromium.org/p/chromium/issues/detail?id=32522#c50
110 #
111 # So basically the options are either to use AI_ADDRCONFIG and then add some
112 # complicated special cases to work around its brokenness, or else don't use
113 # AI_ADDRCONFIG and accept that sometimes on legacy/misconfigured networks
114 # we'll waste 300 ms trying to connect to a blackholed destination.
115 #
116 # Twisted and Tornado always uses default flags. I think we'll do the same.
117
118
119 @contextmanager
120 def close_all() -> Generator[set[SocketType], None, None]:
121 sockets_to_close: set[SocketType] = set()
122 try:
123 yield sockets_to_close
124 finally:
125 errs = []
126 for sock in sockets_to_close:
127 try:
128 sock.close()
129 except BaseException as exc:
130 errs.append(exc)
131 if len(errs) == 1:
132 raise errs[0]
133 elif errs:
134 raise BaseExceptionGroup("", errs)
135
136
137 def reorder_for_rfc_6555_section_5_4( # type: ignore[explicit-any]
138 targets: MutableSequence[tuple[AddressFamily, SocketKind, int, str, Any]],
139 ) -> None:
140 # RFC 6555 section 5.4 says that if getaddrinfo returns multiple address
141 # families (e.g. IPv4 and IPv6), then you should make sure that your first
142 # and second attempts use different families:
143 #
144 # https://tools.ietf.org/html/rfc6555#section-5.4
145 #
146 # This function post-processes the results from getaddrinfo, in-place, to
147 # satisfy this requirement.
148 for i in range(1, len(targets)):
149 if targets[i][0] != targets[0][0]:
150 # Found the first entry with a different address family; move it
151 # so that it becomes the second item on the list.
152 if i != 1:
153 targets.insert(1, targets.pop(i))
154 break
155
156
157 def format_host_port(host: str | bytes, port: int | str) -> str:
158 host = host.decode("ascii") if isinstance(host, bytes) else host
159 if ":" in host:
160 return f"[{host}]:{port}"
161 else:
162 return f"{host}:{port}"
163
164
165 # Twisted's HostnameEndpoint has a good set of configurations:
166 # https://twistedmatrix.com/documents/current/api/twisted.internet.endpoints.HostnameEndpoint.html
167 #
168 # - per-connection timeout
169 # this doesn't seem useful -- we let you set a timeout on the whole thing
170 # using Trio's normal mechanisms, and that seems like enough
171 # - delay between attempts
172 # - bind address (but not port!)
173 # they *don't* support multiple address bindings, like giving the ipv4 and
174 # ipv6 addresses of the host.
175 # I think maybe our semantics should be: we accept a list of bind addresses,
176 # and we bind to the first one that is compatible with the
177 # connection attempt we want to make, and if none are compatible then we
178 # don't try to connect to that target.
179 #
180 # XX TODO: implement bind address support
181 #
182 # Actually, the best option is probably to be explicit: {AF_INET: "...",
183 # AF_INET6: "..."}
184 # this might be simpler after
185 async def open_tcp_stream(
186 host: str | bytes,
187 port: int,
188 *,
189 happy_eyeballs_delay: float | None = DEFAULT_DELAY,
190 local_address: str | None = None,
191 ) -> trio.SocketStream:
192 """Connect to the given host and port over TCP.
193
194 If the given ``host`` has multiple IP addresses associated with it, then
195 we have a problem: which one do we use?
196
197 One approach would be to attempt to connect to the first one, and then if
198 that fails, attempt to connect to the second one ... until we've tried all
199 of them. But the problem with this is that if the first IP address is
200 unreachable (for example, because it's an IPv6 address and our network
201 discards IPv6 packets), then we might end up waiting tens of seconds for
202 the first connection attempt to timeout before we try the second address.
203
204 Another approach would be to attempt to connect to all of the addresses at
205 the same time, in parallel, and then use whichever connection succeeds
206 first, abandoning the others. This would be fast, but create a lot of
207 unnecessary load on the network and the remote server.
208
209 This function strikes a balance between these two extremes: it works its
210 way through the available addresses one at a time, like the first
211 approach; but, if ``happy_eyeballs_delay`` seconds have passed and it's
212 still waiting for an attempt to succeed or fail, then it gets impatient
213 and starts the next connection attempt in parallel. As soon as any one
214 connection attempt succeeds, all the other attempts are cancelled. This
215 avoids unnecessary load because most connections will succeed after just
216 one or two attempts, but if one of the addresses is unreachable then it
217 doesn't slow us down too much.
218
219 This is known as a "happy eyeballs" algorithm, and our particular variant
220 is modelled after how Chrome connects to webservers; see `RFC 6555
221 <https://tools.ietf.org/html/rfc6555>`__ for more details.
222
223 Args:
224 host (str or bytes): The host to connect to. Can be an IPv4 address,
225 IPv6 address, or a hostname.
226
227 port (int): The port to connect to.
228
229 happy_eyeballs_delay (float or None): How many seconds to wait for each
230 connection attempt to succeed or fail before getting impatient and
231 starting another one in parallel. Set to `None` if you want
232 to limit to only one connection attempt at a time (like
233 :func:`socket.create_connection`). Default: 0.25 (250 ms).
234
235 local_address (None or str): The local IP address or hostname to use as
236 the source for outgoing connections. If ``None``, we let the OS pick
237 the source IP.
238
239 This is useful in some exotic networking configurations where your
240 host has multiple IP addresses, and you want to force the use of a
241 specific one.
242
243 Note that if you pass an IPv4 ``local_address``, then you won't be
244 able to connect to IPv6 hosts, and vice-versa. If you want to take
245 advantage of this to force the use of IPv4 or IPv6 without
246 specifying an exact source address, you can use the IPv4 wildcard
247 address ``local_address="0.0.0.0"``, or the IPv6 wildcard address
248 ``local_address="::"``.
249
250 Returns:
251 SocketStream: a :class:`~trio.abc.Stream` connected to the given server.
252
253 Raises:
254 OSError: if the connection fails.
255
256 See also:
257 open_ssl_over_tcp_stream
258
259 """
260
261 # To keep our public API surface smaller, rule out some cases that
262 # getaddrinfo will accept in some circumstances, but that act weird or
263 # have non-portable behavior or are just plain not useful.
264 if not isinstance(host, (str, bytes)):
265 raise ValueError(f"host must be str or bytes, not {host!r}")
266 if not isinstance(port, int):
267 raise TypeError(f"port must be int, not {port!r}")
268
269 if happy_eyeballs_delay is None:
270 happy_eyeballs_delay = DEFAULT_DELAY
271
272 targets = await getaddrinfo(host, port, type=SOCK_STREAM)
273
274 # I don't think this can actually happen -- if there are no results,
275 # getaddrinfo should have raised OSError instead of returning an empty
276 # list. But let's be paranoid and handle it anyway:
277 if not targets:
278 msg = f"no results found for hostname lookup: {format_host_port(host, port)}"
279 raise OSError(msg)
280
281 reorder_for_rfc_6555_section_5_4(targets)
282
283 # This list records all the connection failures that we ignored.
284 oserrors: list[OSError] = []
285
286 # Keeps track of the socket that we're going to complete with,
287 # need to make sure this isn't automatically closed
288 winning_socket: SocketType | None = None
289
290 # Try connecting to the specified address. Possible outcomes:
291 # - success: record connected socket in winning_socket and cancel
292 # concurrent attempts
293 # - failure: record exception in oserrors, set attempt_failed allowing
294 # the next connection attempt to start early
295 # code needs to ensure sockets can be closed appropriately in the
296 # face of crash or cancellation
297 async def attempt_connect(
298 socket_args: tuple[AddressFamily, SocketKind, int],
299 sockaddr: AddressFormat,
300 attempt_failed: trio.Event,
301 ) -> None:
302 nonlocal winning_socket
303
304 try:
305 sock = socket(*socket_args)
306 open_sockets.add(sock)
307
308 if local_address is not None:
309 # TCP connections are identified by a 4-tuple:
310 #
311 # (local IP, local port, remote IP, remote port)
312 #
313 # So if a single local IP wants to make multiple connections
314 # to the same (remote IP, remote port) pair, then those
315 # connections have to use different local ports, or else TCP
316 # won't be able to tell them apart. OTOH, if you have multiple
317 # connections to different remote IP/ports, then those
318 # connections can share a local port.
319 #
320 # Normally, when you call bind(), the kernel will immediately
321 # assign a specific local port to your socket. At this point
322 # the kernel doesn't know which (remote IP, remote port)
323 # you're going to use, so it has to pick a local port that
324 # *no* other connection is using. That's the only way to
325 # guarantee that this local port will be usable later when we
326 # call connect(). (Alternatively, you can set SO_REUSEADDR to
327 # allow multiple nascent connections to share the same port,
328 # but then connect() might fail with EADDRNOTAVAIL if we get
329 # unlucky and our TCP 4-tuple ends up colliding with another
330 # unrelated connection.)
331 #
332 # So calling bind() before connect() works, but it disables
333 # sharing of local ports. This is inefficient: it makes you
334 # more likely to run out of local ports.
335 #
336 # But on some versions of Linux, we can re-enable sharing of
337 # local ports by setting a special flag. This flag tells
338 # bind() to only bind the IP, and not the port. That way,
339 # connect() is allowed to pick the port, and it can do a
340 # better job of it because it knows the remote IP/port.
341 with suppress(OSError, AttributeError):
342 sock.setsockopt(
343 trio.socket.IPPROTO_IP,
344 trio.socket.IP_BIND_ADDRESS_NO_PORT,
345 1,
346 )
347 try:
348 await sock.bind((local_address, 0))
349 except OSError:
350 raise OSError(
351 f"local_address={local_address!r} is incompatible "
352 f"with remote address {sockaddr!r}",
353 ) from None
354
355 await sock.connect(sockaddr)
356
357 # Success! Save the winning socket and cancel all outstanding
358 # connection attempts.
359 winning_socket = sock
360 nursery.cancel_scope.cancel(reason="successfully found a socket")
361 except OSError as exc:
362 # This connection attempt failed, but the next one might
363 # succeed. Save the error for later so we can report it if
364 # everything fails, and tell the next attempt that it should go
365 # ahead (if it hasn't already).
366 oserrors.append(exc)
367 attempt_failed.set()
368
369 with close_all() as open_sockets:
370 # nursery spawns a task for each connection attempt, will be
371 # cancelled by the task that gets a successful connection
372 async with trio.open_nursery() as nursery:
373 for address_family, socket_type, proto, _, addr in targets:
374 # create an event to indicate connection failure,
375 # allowing the next target to be tried early
376 attempt_failed = trio.Event()
377
378 nursery.start_soon(
379 attempt_connect,
380 (address_family, socket_type, proto),
381 addr,
382 attempt_failed,
383 )
384
385 # give this attempt at most this time before moving on
386 with trio.move_on_after(happy_eyeballs_delay):
387 await attempt_failed.wait()
388
389 # nothing succeeded
390 if winning_socket is None:
391 assert len(oserrors) == len(targets)
392 msg = f"all attempts to connect to {format_host_port(host, port)} failed"
393 raise OSError(msg) from ExceptionGroup(msg, oserrors)
394 else:
395 stream = trio.SocketStream(winning_socket)
396 open_sockets.remove(winning_socket)
397 return stream