notes

std::net over the socket contract

An appendix to notes/std.md, which is the page to read. This file holds how TcpStream, TcpListener and UdpSocket map onto net_stack's socket contract, and the port-reuse finding. The directory notes/std/ and this file's stem are provisional names. Naming is calef's.

The records this file cites by number:

std::net over the socket contract (milestone 27 phase two)

sys/net/connection/nife.rs binds std's TcpStream and outbound UdpSocket to net_stack's socket contract (DECISIONS §25, notes/net.md, crates/socket_contract/src/lib.rs). The PAL is a client of the frozen contract, nothing more. It holds the Stack endpoint (slot 2) and a frame untyped (slot 3). An open is a CALL on the Stack rendezvous, answered with the socket's own capability (§255 (each socket is its own capability), since 2026-10-07 UTC). The PAL hands that socket a shared PageFrame it minted and mapped (SEND_CAPABILITY, OPERATION_ATTACH_SHARED_PAGE), and then drives the socket with CALLs on its capability. Control words ride the message; bytes sit in the shared frame. The PAL's socket ids are its own registry's indices and never reach net_stack. This is the exact path the hand-written socket_test_client client walks, reached through std's blocking API instead.

The wire constants are not restated: netproto.rs is generated verbatim from crates/socket_contract/src/lib.rs into sys/pal/nife/netproto.rs by std-src, the same anti-drift discipline as abi.rs. If the contract changes, the PAL's numbers change with it, because there is one source.

What binds, and how it maps to the contract:

The concurrency model is the contract's: single-threaded, one synchronous exchange at a time. A program can hold up to six sockets at once (the PAL's SOCKETS) and interleave them, but there is only ever one operation in flight, which is all a single-threaded process can do anyway. For a listener that means the backlog is one connection deep: the listener re-arms inside ACCEPT, so serving connections one after another works indefinitely, and serving two at once does not.

Capability slots, counted from what the PAL holds (milestone 649 (every client of a network stack shares its socket numbers)). Each open socket costs one slot for its capability. Each PAL id that has ever carried bytes keeps one more, for its page's frame, which the next socket at that id is handed a copy of. So a program holding all six sockets spends twelve of its 64 slots on the network, and one that has closed them all still spends six.

net_stack assigns ephemeral local ports with a rotating allocator, independently of the socket, and a socket has no number at all under §255 (each socket is its own capability). So a socket's local port does not follow its id, and reopening one does not reuse the port it had.

BUGS