tests: the resolver trap ends its threads before it closes its sockets, so its port is free for the next test (no more "Address already in use" on 127.0.0.1-8:53)

The answering threads sit in accept()/recvfrom(); on Linux a close() from another thread does not wake them and the kernel keeps the socket
bound until the call returns, which it never did: every trap left its address taken, and the eighth trap of a run found all eight taken
(reproduced here in a container: 19 DoH e2e, the 6 tests after the 8th trap are errors; alone, the same file passes). The loops now wake every
0.2 s to look at a flag; the teardown sets it, joins the threads, and then closes the sockets.

- test_a_closed_trap_gives_its_port_back_at_once (wrapper.unit.udp 139 -> 140): a trap on a free port is closed and a second one on the same
  port must bind 127.0.0.1 again; red on the old code on Linux.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
feder-crandClaude Sonnet 5.5 committed 2026-11-12 08:42:36 +01:00
1 parent bfb67e0c2c
commit 4933f8cdba
3 files changed
+42 -4

No files matched your search

+1 -1
View File
@@ -2,7 +2,7 @@
{"id": "wrapper.e2e.udp", "giro": "G2", "os": ["linux", "windows"], "min_tests": 41, "exact": true, "tests": ["tests/test_the_association_is_ready_when_the_peerconnection_is_born_e2e.py", "tests/test_turn_over_tcp_is_tried_for_real_e2e.py", "tests/test_webrtc_realness.py::test_the_gather_is_real_without_a_proxy", "tests/test_the_whole_udp_chain_by_name_e2e.py", "tests/test_declared_host_network_e2e.py", "tests/test_a_page_over_ipv6_gathers_on_a_machine_declared_without_it_e2e.py", "tests/test_webrtc_realness.py::test_a_gateway_with_coherent_udp_gives_the_page_a_srflx_born_through_its_relay", "tests/test_webrtc_realness.py::test_a_host_with_ipv6_addresses_gathers_through_an_ipv4_relay", "tests/test_webrtc_realness.py::test_a_gateway_that_is_far_away_still_gives_the_page_a_srflx_born_through_its_relay", "tests/test_webrtc_realness.py::test_the_first_candidate_of_a_gather_does_not_wait_for_a_gateway_that_is_far_away", "tests/test_webrtc_realness.py::test_a_gather_after_the_page_loaded_through_the_gateway_costs_only_the_road_to_the_server", "tests/test_webrtc_realness.py::test_two_contexts_with_two_proxies_each_gets_the_srflx_of_its_own", "tests/test_webrtc_realness.py::test_the_turn_over_tcp_of_a_context_goes_through_the_proxy_of_that_context", "tests/test_webrtc_realness.py::test_a_proxy_that_does_not_relay_udp_gives_no_srflx_at_all", "tests/test_webrtc_realness.py::test_a_proxy_whose_udp_leaves_from_another_address_gives_no_srflx_at_all", "tests/test_webrtc_realness.py::test_a_proxy_whose_relay_never_answers_gives_no_srflx_at_all"], "marker": "e2e", "needs_root": false, "note": "Needs the candidate binary (STEALTHFOX_E2E_BINARY) and the engine that carries the UDP line. 57 = 1+7+6+1+21 from the five files + 21 from the 10 UDP-line tests of test_webrtc_realness.py (parametrized)."},
{"id": "wrapper.e2e.web", "giro": "G2", "os": ["linux", "windows"], "min_tests": 27, "exact": true, "tests": ["tests/test_networkidle_through_a_served_browser_with_routes_e2e.py", "tests/test_http3_through_the_proxy_e2e.py"], "marker": "e2e", "needs_root": false, "note": "The networkidle e2e with routes and the HTTP/3 e2e, split from wrapper.e2e.udp (about 260 s of 500 on Linux; the whole suite did not fit the 15 minutes of Windows)."},
{"id": "wrapper.e2e.doh", "giro": "G2", "os": ["linux", "windows"], "min_tests": 19, "exact": true, "tests": ["tests/test_the_hosts_file_of_the_host_is_never_read_e2e.py", "tests/test_resolver_race_e2e.py", "tests/test_doh_under_stress_e2e.py", "tests/test_context_proxy_only_resolves_by_doh_e2e.py", "tests/test_doh_through_the_proxy_e2e.py", "tests/test_ice_names_by_the_context_proxy_e2e.py"], "marker": "e2e", "needs_root": ["linux"], "note": "Linux needs root: the resolver trap of the file (ResolverTrap) points /etc/resolv.conf at a counting resolver on port 53. Su Windows il resolver trap non gira: l'asserzione sul resolver di sistema è l'unica omessa. Run it without xdist (port 53 is one per host). On the Linux leg the gate exports INVPW_REQUIRE_TRAP=1: a trap that cannot run or bind port 53 is a failure, not a skipped assertion."},
{"id": "wrapper.unit.udp", "giro": "G1", "os": ["linux", "windows"], "min_tests": 139, "exact": true, "tests": ["tests/test_the_h3_line_helpers.py", "tests/test_the_fake_doh_delays_and_refuses.py", "tests/test_the_host_address_helper.py", "tests/test_the_udp_chain_helper.py", "tests/test_the_fake_doh_answers.py", "tests/test_doh_session_prefs_unit.py", "tests/test_the_leak_watcher_sees_a_direct_connection.py", "tests/test_the_http_proxy_helper_asks_for_credentials.py", "tests/test_every_route_command_is_answered_may_fail.py", "tests/test_the_network_declaration_reaches_the_engine.py", "tests/test_the_resolver_trap_is_required_by_the_gate.py"], "marker": "unit", "needs_root": false, "note": "92 = 6+32+21+7+8+3+9+6. The leak watcher file defines exactly one of its two resolver-trap tests per host (7 everywhere), so no test is skipped. `exact`: min_tests is raised by hand when a test is added."},
{"id": "wrapper.unit.udp", "giro": "G1", "os": ["linux", "windows"], "min_tests": 140, "exact": true, "tests": ["tests/test_the_h3_line_helpers.py", "tests/test_the_fake_doh_delays_and_refuses.py", "tests/test_the_host_address_helper.py", "tests/test_the_udp_chain_helper.py", "tests/test_the_fake_doh_answers.py", "tests/test_doh_session_prefs_unit.py", "tests/test_the_leak_watcher_sees_a_direct_connection.py", "tests/test_the_http_proxy_helper_asks_for_credentials.py", "tests/test_every_route_command_is_answered_may_fail.py", "tests/test_the_network_declaration_reaches_the_engine.py", "tests/test_the_resolver_trap_is_required_by_the_gate.py"], "marker": "unit", "needs_root": false, "note": "92 = 6+32+21+7+8+3+9+6. The leak watcher file defines exactly one of its two resolver-trap tests per host (7 everywhere), so no test is skipped. `exact`: min_tests is raised by hand when a test is added."},
{"id": "wrapper.e2e.page", "giro": "G2", "os": ["linux", "windows"], "min_tests": 114, "exact": true, "tests": ["tests/test_a_page_that_moves_on_e2e.py", "tests/test_a_popup_is_reported_when_the_engine_says_it_is_ready_as_playwright_does_e2e.py", "tests/test_a_popup_of_a_link_reaches_the_client_e2e.py", "tests/test_popup_destinations_e2e.py", "tests/test_popups_reach_the_client_e2e.py", "tests/test_route_superseded_e2e.py"], "marker": "e2e", "needs_root": false, "note": "114 = 100 + the 14 of test_a_page_that_moves_on_e2e.py (a navigation the page overtook with location.replace ends; the engine fix 860ef8a6d9 is proved by it). 100 counted at the collection; was 96 = 10+32+16+34+4 before the popup tests of 6200bc2). A page is handed over when the engine says Page.ready; a popup reaches the client (link, noopener, window.open; own site and another; with and without a route; in-process and over launch_server) as Playwright's does; a superseded route. Needs the candidate binary, and the stock Playwright client of the dev extra."},
{"id": "wrapper.unit.page", "giro": "G1", "os": ["linux", "windows"], "min_tests": 24, "exact": true, "tests": ["tests/test_ci_drive_gate_goes_through_the_core.py", "tests/test_a_page_is_handed_over_only_when_the_engine_says_it_is_ready.py", "tests/test_a_new_page_waits_for_the_engine.py", "tests/test_page_number_follows_the_call.py"], "marker": "unit", "needs_root": false, "note": "24 (counted at the collection; was 20 = 3+8+1+8 before 6200bc2). The page dispatcher's wait for Page.ready and what it hands over; the drive gate goes through the core (the engine waits for the session's road)."}
]
+22 -3
View File
@@ -212,6 +212,9 @@ class ResolverTrap:
self.queries: List[DohQuery] = []
self._lock = threading.Lock()
self._sockets: List[socket.socket] = []
#: The threads that answer on `_sockets`, and the flag that ends them: see `_restore`.
self._threads: List[threading.Thread] = []
self._ending = threading.Event()
self._original: Optional[bytes] = None
self._running = False
self._redirects: List[List[str]] = []
@@ -287,6 +290,15 @@ class ResolverTrap:
with open(RESOLV_CONF, "wb") as f:
f.write(self._original)
self._original = None
# ⛔ THE THREADS END BEFORE THE SOCKETS ARE CLOSED. A thread blocked in accept() or recvfrom() is not woken by a close()
# of its socket from another thread (Linux): the kernel keeps the socket, still bound, until the call returns, and the
# call never returns. The port stayed taken after the test, and the NEXT trap found 127.0.0.1-8:53 "already in use"
# one address per test, until all eight were gone (run 43164943499; here: the eighth trap of a run of 19 tests).
# The answering loops therefore wake up every 0.2 s to look at `_ending`, and the socket is closed once they are gone.
self._ending.set()
for t in self._threads:
t.join(2.0)
self._threads = []
for s in self._sockets:
try:
s.close()
@@ -318,8 +330,11 @@ class ResolverTrap:
s.close()
self._sockets += made
for s in made:
s.settimeout(0.2)
target = self._serve_udp if s.type == socket.SOCK_DGRAM else self._serve_tcp
threading.Thread(target=target, args=(s,), daemon=True).start()
t = threading.Thread(target=target, args=(s,), daemon=True)
self._threads.append(t)
t.start()
def _bind_once(self, discarded: List[socket.socket], address: str = "127.0.0.1") -> List[socket.socket]:
"""One attempt: the sockets bound, or [] after putting what it opened in ``discarded``."""
@@ -365,9 +380,11 @@ class ResolverTrap:
return build_response(query, RCODE_REFUSED)
def _serve_udp(self, sock: socket.socket) -> None:
while True:
while not self._ending.is_set():
try:
data, peer = sock.recvfrom(4096)
except socket.timeout:
continue
except OSError:
return
response = self._respond(data, "udp")
@@ -378,9 +395,11 @@ class ResolverTrap:
pass
def _serve_tcp(self, listener: socket.socket) -> None:
while True:
while not self._ending.is_set():
try:
conn, _ = listener.accept()
except socket.timeout:
continue
except OSError:
return
threading.Thread(target=self._tcp_conversation, args=(conn,), daemon=True).start()
@@ -84,3 +84,22 @@ def test_the_no_proxy_test_fails_when_the_gate_requires_the_trap_and_it_cannot_r
test = module.test_a_session_without_a_proxy_resolves_with_the_system_resolver
with pytest.raises(pytest.fail.Exception, match="no root here"):
test("unused-binary", None, None)
def test_a_closed_trap_gives_its_port_back_at_once():
"""The threads that answer on the trap's sockets end BEFORE the sockets are closed. On Linux a close() from another thread
does not wake a thread blocked in accept()/recvfrom(): the kernel kept the socket, still bound, so the port stayed taken after
the test and the next trap went to 127.0.0.2, then 127.0.0.3 ... until none of the eight was left ("Address already in use" on
all of them, run 43164943499). The old code fails here on Linux; no root is needed (a free port, no resolv.conf)."""
first = ResolverTrap(port=0, rewrite_resolv_conf=False)
first.__enter__()
port = first.port
assert port and first.address == "127.0.0.1"
first.__exit__(None, None, None)
again = ResolverTrap(port=port, rewrite_resolv_conf=False)
try:
again.__enter__()
assert again.address == "127.0.0.1", (
"the port %d of the closed trap was still taken: the second trap had to go to %s" % (port, again.address))
finally:
again.__exit__(None, None, None)