Skip to main content

Module default_port

Module default_port 

Source
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:

  1. An explicit port is never replaced — 0 included. This module is consulted only when the caller passed no port at all.
  2. The protocol’s declared ProtocolMetadataV2::well_known_port when 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).
  3. 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§

DefaultPort
The resolved default for one server start.

Enums§

PortFallback
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 None when a port means nothing to it (an interface, a pipe, a pty).
port_is_in_use
Whether host:port is already held by someone else, for the given transport.
resolve_default_port
Resolve the default port from a declared well-known port.