CoplayDev/unity-mcp · error · ArgumentOutOfRangeException
Port must be positive.
Error message
Port must be positive.
What it means
SetPreferredPort rejects non-positive ports immediately with ArgumentOutOfRangeException ('port <= 0') before any availability probe. This is an argument guard ensuring a valid port number is supplied.
Source
Thrown at MCPForUnity/Editor/Helpers/PortManager.cs:94
/// <summary>
/// Decide whether a configured port that keeps reporting AddressAlreadyInUse should be
/// abandoned for a freshly discovered port. Returns false (keep retrying the same port)
/// until it has been continuously busy for <see cref="BusyPortFallbackWindowSeconds"/>,
/// which distinguishes a domain-reload socket-release race from a foreign occupant (#1173).
/// </summary>
public static bool ShouldAbandonBusyPort(double busyForSeconds)
=> busyForSeconds >= BusyPortFallbackWindowSeconds;
/// <summary>
/// Persist a user-selected port and return the value actually stored.
/// If <paramref name="port"/> is unavailable, the next available port is chosen instead.
/// </summary>
public static int SetPreferredPort(int port)
{
if (port <= 0)
{
throw new ArgumentOutOfRangeException(nameof(port), "Port must be positive.");
}
if (!IsPortAvailable(port))
{
throw new InvalidOperationException($"Port {port} is already in use.");
}
SavePort(port);
return port;
}
/// <summary>
/// Find an available port starting from the default port
/// </summary>
/// <returns>Available port number</returns>
private static int FindAvailablePort()
{
// Always try default port firstView on GitHub (pinned to c21bf496bc)
Solutions
- Pass a positive port in the valid range, typically 1024-65535.
- Validate and normalize the configured port (defaulting to the project default) before calling SetPreferredPort.
- Distinguish 'unset' (use auto-find) from 'explicit invalid' (surface a config error).
Example fix
// before SetPreferredPort(parsedPort); // parsedPort may be 0 when config missing // after int port = parsedPort > 0 ? parsedPort : PortManager.DefaultPort; SetPreferredPort(port);
Defensive patterns
Strategy: validation
Validate before calling
int NormalizePort(int candidate, int fallback)
{
if (candidate <= 0 || candidate > 65535) return fallback;
return candidate;
}
int port = NormalizePort(configPort, PortManager.DefaultPort);
if (port <= 0) throw new InvalidOperationException("No valid port configured.");
PortManager.SetPreferredPort(port); Type guard
static bool IsValidPort(int port) => port > 0 && port <= 65535;
Try / catch
try { PortManager.SetPreferredPort(port); }
catch (ArgumentOutOfRangeException)
{
// Config supplied an invalid port; fall back to the default and retry.
PortManager.SetPreferredPort(PortManager.DefaultPort);
} Prevention
- Treat an unset port as 'use default/auto-find' rather than forwarding 0.
- Bound user-supplied ports to 1..65535 before calling SetPreferredPort.
- Surface a clear config error when the parsed port is non-positive.
When it happens
Trigger: Calling SetPreferredPort with 0, a negative number, or an uninitialized int (default 0) from missing config. The check is 'if (port <= 0)'.
Common situations: Port config unset and defaulting to 0; a parse error producing 0; off-by-one in a port-range computation; a null/empty config value coerced to 0.
Related errors
- Port {port} is already in use.
- No available ports found in range {DefaultPort}-{DefaultPort
- uid required
- zipPath required
- No Unity instance found on port {value}. Available: {availab
AI-assisted analysis of CoplayDev/unity-mcp@c21bf496bc (2026-08-13).
Data as JSON: /api/errors/3124007f2e4ecf41.
Report an issue: GitHub.