CoplayDev/unity-mcp · error · Exception

No available ports found in range {DefaultPort}-{DefaultPort

Error message

No available ports found in range {DefaultPort}-{DefaultPort + MaxPortAttempts}

What it means

FindAvailablePort scans from DefaultPort+1 up to (but not including, due to '<' vs '<=') DefaultPort+MaxPortAttempts and returns the first free port. If none in that window are available, it throws a generic Exception naming the scanned range. This is the fallback when the default port itself is busy.

Source

Thrown at MCPForUnity/Editor/Helpers/PortManager.cs:131

            if (IsPortAvailable(DefaultPort))
            {
                if (IsDebugEnabled()) McpLog.Info($"Using default port {DefaultPort}");
                return DefaultPort;
            }

            if (IsDebugEnabled()) McpLog.Info($"Default port {DefaultPort} is in use, searching for alternative...");

            // Search for alternatives
            for (int port = DefaultPort + 1; port < DefaultPort + MaxPortAttempts; port++)
            {
                if (IsPortAvailable(port))
                {
                    if (IsDebugEnabled()) McpLog.Info($"Found available port {port}");
                    return port;
                }
            }

            throw new Exception($"No available ports found in range {DefaultPort}-{DefaultPort + MaxPortAttempts}");
        }

        /// <summary>
        /// Check if a specific port is available for binding
        /// </summary>
        /// <param name="port">Port to check</param>
        /// <returns>True if port is available</returns>
        public static bool IsPortAvailable(int port)
        {
            try
            {
                var testListener = new TcpListener(IPAddress.Loopback, port);
#if UNITY_EDITOR_OSX
                // On macOS, SO_REUSEADDR (the default) lets multiple processes bind the same
                // port — including AssetImportWorkers. ExclusiveAddressUse prevents this so
                // the test bind fails when another process already holds the port.
                try { testListener.Server.ExclusiveAddressUse = true; } catch { }
#endif

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Free up ports within the scanned range by stopping unneeded listeners.
  2. Raise MaxPortAttempts or change DefaultPort in configuration if the value is configurable.
  3. Reduce the number of concurrent Unity/Server instances competing for ports.
Defensive patterns

Strategy: retry

Validate before calling

// Count free ports in the candidate window before relying on FindAvailablePort.
int free = Enumerable.Range(PortManager.DefaultPort + 1, PortManager.MaxPortAttempts - 1)
    .Count(PortManager.IsPortAvailable);
if (free == 0)
    throw new InvalidOperationException("No free ports in the scan window; reduce running listeners.");

Try / catch

try { return PortManager.FindAvailablePort(); }
catch (Exception ex) when (ex.Message.Contains("No available ports"))
{
    // Surface actionable guidance: free ports or widen the window.
    throw new InvalidOperationException("Port range exhausted; stop other listeners or extend MaxPortAttempts.", ex);
}

Prevention

When it happens

Trigger: The default port is busy AND every port in the scan window (DefaultPort+1 .. DefaultPort+MaxPortAttempts-1) is also occupied. A heavily loaded machine or a restricted port range triggers it.

Common situations: Many local dev servers/listeners running; ephemeral-port exhaustion; OS/firewall reserving a wide range; MaxPortAttempts too small for the workload.

Related errors


AI-assisted analysis of CoplayDev/unity-mcp@c21bf496bc (2026-08-13). Data as JSON: /api/errors/19c0ae592278a91b. Report an issue: GitHub.