1 suggestion
Optional improvements
[suggestion] · Mocked HTTP test leaves BSD source-port selection unchecked
The new TestHTTPConnection checks option values and call order, but does not verify that a real BSD connection receives a source port in the high range.
Location: test/functional/test_framework/netutil.py:290 (head).
Details and suggested fix
Mocking socket.socket means this test passes even if the expected option tuple does not produce the intended kernel behavior. Its independent constants are useful: changing only the production constants would fail the test. Keep that check and add a BSD-only integration test that opens a loopback listener on port zero, connects with netutil.HTTPConnection, and checks conn.sock.getsockname()[1] >= 49152 for IPv4 and IPv6. With the documented default ranges, OpenBSD's ordinary allocation falls entirely below that boundary. FreeBSD's default range overlaps the high range, so repeated connections improve detection without guaranteeing it. Skip this check elsewhere and account for locally customized BSD range settings.
Background
Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.
Documented decisions
The BSD header documentation quoted in the original issue describes automatic local-port selection for both bind() and connect(), and describes IP_PORTRANGE_HIGH as selecting a high range reserved for outbound client connections.
The historical rationale was therefore not inherently limited to explicit bind(port=0). It supplies a direct conceptual basis for applying the option before an outgoing HTTP socket's connect(), when that operation causes the kernel to select its source port.