|
| 1 | +"""Local restart probes must distinguish TIME_WAIT from an occupied listener.""" |
| 2 | + |
| 3 | +import errno |
| 4 | +import socket |
| 5 | +import sys |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from rlm.v100.serving import ensure_local_port_available |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize("host", ["127.0.0.1", "0.0.0.0"]) |
| 13 | +def test_live_listener_still_blocks_reusable_probe(host): |
| 14 | + with socket.socket() as listener: |
| 15 | + listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 16 | + listener.bind((host, 0)) |
| 17 | + listener.listen(1) |
| 18 | + port = listener.getsockname()[1] |
| 19 | + with pytest.raises(OSError, match=f"port {port} unavailable") as failure: |
| 20 | + ensure_local_port_available(port) |
| 21 | + assert failure.value.errno == errno.EADDRINUSE |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.skipif(sys.platform != "linux", reason="Linux TIME_WAIT restart regression") |
| 25 | +def test_restart_after_server_closes_connection_does_not_need_kernel_timeout(): |
| 26 | + with socket.socket() as listener, socket.socket() as client: |
| 27 | + listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 28 | + listener.bind(("127.0.0.1", 0)) |
| 29 | + listener.listen(1) |
| 30 | + port = listener.getsockname()[1] |
| 31 | + client.connect(("127.0.0.1", port)) |
| 32 | + accepted, _ = listener.accept() |
| 33 | + # Server closes first, so its local endpoint enters TIME_WAIT. |
| 34 | + accepted.shutdown(socket.SHUT_WR) |
| 35 | + assert client.recv(1) == b"" |
| 36 | + client.close() |
| 37 | + accepted.close() |
| 38 | + with socket.socket() as old_probe: |
| 39 | + with pytest.raises(OSError) as failure: |
| 40 | + old_probe.bind(("127.0.0.1", port)) |
| 41 | + assert failure.value.errno == errno.EADDRINUSE |
| 42 | + ensure_local_port_available(port) |
| 43 | + # The probe itself leaves no resident listener behind. |
| 44 | + with socket.socket() as restarted: |
| 45 | + restarted.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 46 | + restarted.bind(("127.0.0.1", port)) |
| 47 | + restarted.listen(1) |
0 commit comments