1 suggestion
Optional improvements
[suggestion] · Mocked HTTP test leaves BSD source-port allocation unchecked
The mocked test verifies socket-option ordering but not actual source-port allocation; a supplemental BSD-only loopback check would cover whether the kernel applies the option during implicit binding.
Location: test/functional/test_framework/netutil.py:300 (head).
Details and suggested fix
test_bsd_port_range_before_connect mocks the platform, address resolution and socket. It catches a missing option, incorrect ordering or a production constant changed independently of the test, but cannot establish that the kernel honors the option during connect(). That is the new behavior relied upon here; the existing helper previously covered explicit binding.
Add a BSD-only loopback check using HTTPConnection without an explicit source address, covering IPv4 and IPv6 and inspecting conn.sock.getsockname()[1]. Under default ranges, assert a source port of at least 49152, while accommodating sysctl-customized ranges. OpenBSD's default range is disjoint from the high range, whereas FreeBSD's overlaps it, so several connections would provide only probabilistic detection of an ignored option there. This would benefit local BSD runs rather than the FreeBSD cross-build, which disables both unit and functional tests.
Background
Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.
Documented decisions
The safeguard was introduced to separate two competing allocators: the framework's deterministic test ports and FreeBSD's kernel-assigned ephemeral ports. The original proposal identified FreeBSD's default 10000–65535 range as overlapping the framework's approximately 11000–26000 range, causing intermittent address-in-use failures. IP_PORTRANGE_HIGH requested 49152–65535 on individual sockets before binding, avoiding that overlap.
This is a test-reliability and allocation-isolation rationale, not an HTTP-specific requirement. The pinned allocator still reserves three 5000-port bands beginning at 11000 by default, so the historical separation argument remains numerically applicable under those defaults.
Address-family-specific socket options were an explicit compatibility requirement. During the original work, an invalid-argument failure was traced to IPv6 sockets: FreeBSD required IPV6_PORTRANGE at IPPROTO_IPV6 rather than the IPv4 option.
An HTTP hostname can resolve to IPv4 or IPv6. The existing helper preserves this distinction, and the new connection factory invokes it on each resolved socket before connecting. Thus the historical platform requirement applies to internally created HTTP sockets just as it did to explicitly managed sockets.
Ralph's inferences
The pinned safeguard assumes FreeBSD and OpenBSD need special treatment: its platform gate covers those systems, and its documentation identifies overlapping default ephemeral ranges of 10000–65535 and 1024–49151 respectively. This is a targeted platform assumption, not evidence that every supported operating system needs the option.
The HTTP extension retains the same platform gate, option constants, address-family handling, and assumed high range. It extends where the existing policy is applied rather than changing that policy.
The port-separation argument carries over to implicitly bound HTTP clients provided the BSD option controls source-port selection during connect(): the relevant event is the kernel choosing a local port, not whether Python code explicitly calls bind(). The default test bands remain below the documented high range, but that conclusion is conditional on the configured ranges remaining disjoint.
At the base, AuthServiceProxy delegates HTTP socket creation to the standard HTTPConnection. At the head, it uses a subclass whose connection factory applies the existing option before an optional bind and before connect(). This places the safeguard before implicit source-port allocation without requiring a system-wide range change. The included mocked tests establish call ordering for both address families, not real-kernel allocation or self-connection behavior.