diff options
| author | Dennis Fink | 2026-11-13 20:03:49 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-13 20:03:49 +0100 |
| commit | 87117345ed6e99e5a070d919ac17139cba7b8fe4 (patch) | |
| tree | d7df5d45008dd2959194e2614b87333598b85198 /webmentions_ssg/net.py | |
| parent | 2fbe1d383650cb31f3ef8d58c04e4d4095935833 (diff) | |
| download | webmentions-ssg-87117345ed6e99e5a070d919ac17139cba7b8fe4.tar.gz webmentions-ssg-87117345ed6e99e5a070d919ac17139cba7b8fe4.zip | |
fix(core): make network task failures explicit
Introduce a dedicated response-size error so callers only translate
expected limit violations instead of masking unrelated `ValueError`
exceptions.
Ensure failed sockets are always closed, remove dead discovery error
handling, and log unexpected sender failures with tracebacks before
recording them.
Diffstat (limited to '')
| -rw-r--r-- | webmentions_ssg/net.py | 81 |
1 files changed, 49 insertions, 32 deletions
diff --git a/webmentions_ssg/net.py b/webmentions_ssg/net.py index 9a6ecfb..0314810 100644 --- a/webmentions_ssg/net.py +++ b/webmentions_ssg/net.py @@ -8,6 +8,7 @@ import ipaddress import socket import time from collections.abc import Iterable +from contextlib import ExitStack from urllib.parse import urlsplit import dns.exception @@ -29,6 +30,10 @@ class UnsafeAddressError(ValueError): """A hostname or IP literal identifies a non-public network address.""" +class ResponseTooLargeError(ValueError): + """A response body exceeds its configured size limit.""" + + class NetworkDeadline: """One monotonic deadline shared by DNS, TCP, TLS, and response streaming.""" @@ -89,6 +94,43 @@ class PublicNetworkBackend(httpcore.SyncBackend): def __init__(self, deadline: NetworkDeadline): self._deadline = deadline + def _connect_one( + self, + address: str, + port: int, + timeout: float | None, + local_address: str | None, + socket_options: Iterable[tuple] | None, + ) -> httpcore.NetworkStream: + family = ( + socket.AF_INET6 + if ipaddress.ip_address(address).version == 6 + else socket.AF_INET + ) + + # Close the socket on any error, transferring ownership to SyncStream + # only after the connection and stream have been created successfully. + with ExitStack() as cleanup: + sock = socket.socket(family, socket.SOCK_STREAM) + cleanup.callback(sock.close) + sock.settimeout(self._deadline.timeout(timeout, httpcore.ConnectTimeout)) + + if local_address is not None: + sock.bind((local_address, 0)) + + for option in socket_options or (): + sock.setsockopt(*option) + + sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) + + # This numeric address is the *same* address just validated; + # no getaddrinfo or DNS resolver runs between check and connect. + sock.connect((address, port)) + + stream = DeadlineStream(SyncStream(sock), self._deadline) + cleanup.pop_all() + return stream + def connect_tcp( self, host: str, @@ -104,43 +146,18 @@ class PublicNetworkBackend(httpcore.SyncBackend): last_error: httpcore.ConnectError | httpcore.ConnectTimeout | None = None for address in addresses: - family = ( - socket.AF_INET6 - if ipaddress.ip_address(address).version == 6 - else socket.AF_INET - ) - sock = socket.socket(family, socket.SOCK_STREAM) - try: - sock.settimeout( - self._deadline.timeout(timeout, httpcore.ConnectTimeout) + return self._connect_one( + address, port, timeout, local_address, socket_options ) - - if local_address is not None: - sock.bind((local_address, 0)) - - for option in socket_options or (): - sock.setsockopt(*option) - - sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) - - # This numeric address is the *same* address just validated; - # no getaddrinfo or DNS resolver runs between check and connect. - sock.connect((address, port)) - - return DeadlineStream(SyncStream(sock), self._deadline) - except (TimeoutError, OSError) as exc: - sock.close() - + except OSError as exc: if isinstance(exc, TimeoutError): last_error = httpcore.ConnectTimeout(str(exc)) else: last_error = httpcore.ConnectError(str(exc)) - except BaseException: - sock.close() - raise - assert last_error is not None + if last_error is None: + raise httpcore.ConnectError(f"No public addresses available for {host!r}") raise last_error @@ -256,13 +273,13 @@ def read_limited_body(response: httpx.Response, max_bytes: int, *, label: str) - pass else: if declared_length > max_bytes: - raise ValueError(f"{label} document is too large") + raise ResponseTooLargeError(f"{label} document is too large") body = bytearray() for chunk in response.iter_bytes(chunk_size=64 * 1024): body.extend(chunk) if len(body) > max_bytes: - raise ValueError(f"{label} document is too large") + raise ResponseTooLargeError(f"{label} document is too large") return bytes(body) |
