Expand description
The port a server starts on when the caller names none.
One decision, used everywhere a port can be left out: the dashboard’s picker and create
form, ServerForm, MCP start_server, the model’s open_server and --server. All of
them reach it through crate::cli::server_startup::start_server_from_action or, for the
picker’s preview, through default_port_for_server directly, so the port a preview names
is the port a start takes.
The rule:
- An explicit port is never replaced —
0included. This module is consulted only when the caller passed no port at all. - The protocol’s declared
ProtocolMetadataV2::well_known_portwhen it is 1024 or above, or when it is below 1024 and this process can bind privileged ports (SystemCapabilities::can_bind_privileged_ports, the existing probe — not a new one). - Otherwise
0, an OS-assigned port, with the reason: the privileged port this process cannot bind, or the port something else already holds.
A port already taken falls back rather than failing, because the caller who named no port asked for “a” server, not for that number — and the one who needs the number passes it, in which case a taken port is an error they should see.
The in-use check binds and drops a probe socket on the address the server would use. That
is the probe-then-bind pattern server_startup.rs removed for port 0, and it is safe here
for the reason it was not there: a fixed port cannot be handed to someone else between the
probe and the real bind. If another process takes it in that window, the real bind fails
with its own “address in use”, which is the same error the caller would have had anyway.
Structs§
- Default
Port - The resolved default for one server start.
Enums§
- Port
Fallback - Why a server is not starting on its well-known port.
Constants§
- DEFAULT_
HOST - The host a socket server binds when the caller and the protocol name none.
Functions§
- default_
port_ for_ server - The default port for a server protocol when the caller names none, or
Nonewhen a port means nothing to it (an interface, a pipe, a pty). - port_
is_ in_ use - Whether
host:portis already held by someone else, for the given transport. - resolve_
default_ port - Resolve the default port from a declared well-known port.