wait_port_listen() searches anywhere in each /proc/net/{tcp,udp}* row. For UDP, a connected client whose remote port matches the requested port therefore makes the helper return even if no local receiver is bound. Callers waiting for a UDP server can consequently continue too early. Anchor the pattern to the start of the row and consume the slot and local address before matching the port. Consume the remote address before the TCP state, and terminate the LISTEN state match at a word boundary. Keep accepting locally bound connected UDP sockets. For example, in an isolated network namespace with loopback up, run this from tools/testing/selftests/net, with port 12345 unused locally: import socket from lib.py import wait_port_listen s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("127.0.0.1", 12345)) wait_port_listen(12345, proto="udp", deadline=0.1) The original helper returns; the fixed helper times out as expected. AI-assisted review found this issue. Real IPv4/IPv6 socket tests covered absent, remote-only, bound and connected UDP ports, and absent, bound, established and listening TCP sockets: the original failed two of 16 cases, and the fixed helper passed all 16. No hardware tests were run. No kernel instrumentation or fault injection was used. Fixes: 31611cea8f0f ("selftests: drv-net: add a TCP ping test case (and useful helpers)") Signed-off-by: Jingjing Xu --- tools/testing/selftests/net/lib/py/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/net/lib/py/utils.py b/tools/testing/selftests/net/lib/py/utils.py index 1727bc418073..d3877f8bc2df 100644 --- a/tools/testing/selftests/net/lib/py/utils.py +++ b/tools/testing/selftests/net/lib/py/utils.py @@ -342,9 +342,9 @@ def rand_ports(count, stype=socket.SOCK_STREAM): def wait_port_listen(port, proto="tcp", ns=None, host=None, sleep=0.005, deadline=5): end = time.monotonic() + deadline - pattern = f":{port:04X} .* " + pattern = rf"^\s*\d+:\s+\S+:{port:04X}\s+\S+\s+" if proto == "tcp": # for tcp protocol additionally check the socket state - pattern += "0A" + pattern += r"0A\b" pattern = re.compile(pattern) while True: base-commit: 2208ab00f4f213ade91ab775e7f8c4dacdcc856e -- 2.53.0