Files
lneto/x/xnet
Derek den Haas 6313b1570d fix(xnet): allocate ephemeral ports sequentially, not randomly (#180)
Ephemeral ports were drawn at random from the 16384-port dynamic range on
every dial. Under connection-per-request churn the birthday paradox reuses
a recently-released port after only a few dozen dials (~1% per dial at 200
outstanding-in-teardown), and a reused 4-tuple lands on whatever state the
previous conversation left along the path, such as the peer's TIME-WAIT
socket or a NAT's flow-table entry, which silently swallows the new SYN.
Measured end to end: an HTTP client with keep-alives off against a macOS
peer hit a dead dial after ~286 connections and stayed dark for ~30
seconds, exactly one 2MSL TIME-WAIT expiry.

Allocate sequentially from a per-stack random starting offset instead: a
port is only revisited after the full 16384-port cycle, and the random
start keeps a rebooted node off the ports its previous life just used.

TestEphemeralPortSequence pins the full-cycle-no-reuse property.

Co-authored-by: Derek den Haas <d.haas@directcode.com>
2026-08-24 19:39:46 -03:00
..
2026-04-11 18:01:05 -03:00
2026-04-14 19:11:29 -03:00