{"record":{"id":"19c0ae592278a91b","repo":"CoplayDev/unity-mcp","slug":"no-available-ports-found-in-range-defaultport-d","errorCode":null,"errorMessage":"No available ports found in range {DefaultPort}-{DefaultPort + MaxPortAttempts}","messagePattern":"No available ports found in range (.+?)-(.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"MCPForUnity/Editor/Helpers/PortManager.cs","lineNumber":131,"sourceCode":"            if (IsPortAvailable(DefaultPort))\n            {\n                if (IsDebugEnabled()) McpLog.Info($\"Using default port {DefaultPort}\");\n                return DefaultPort;\n            }\n\n            if (IsDebugEnabled()) McpLog.Info($\"Default port {DefaultPort} is in use, searching for alternative...\");\n\n            // Search for alternatives\n            for (int port = DefaultPort + 1; port < DefaultPort + MaxPortAttempts; port++)\n            {\n                if (IsPortAvailable(port))\n                {\n                    if (IsDebugEnabled()) McpLog.Info($\"Found available port {port}\");\n                    return port;\n                }\n            }\n\n            throw new Exception($\"No available ports found in range {DefaultPort}-{DefaultPort + MaxPortAttempts}\");\n        }\n\n        /// <summary>\n        /// Check if a specific port is available for binding\n        /// </summary>\n        /// <param name=\"port\">Port to check</param>\n        /// <returns>True if port is available</returns>\n        public static bool IsPortAvailable(int port)\n        {\n            try\n            {\n                var testListener = new TcpListener(IPAddress.Loopback, port);\n#if UNITY_EDITOR_OSX\n                // On macOS, SO_REUSEADDR (the default) lets multiple processes bind the same\n                // port — including AssetImportWorkers. ExclusiveAddressUse prevents this so\n                // the test bind fails when another process already holds the port.\n                try { testListener.Server.ExclusiveAddressUse = true; } catch { }\n#endif","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/CoplayDev/unity-mcp/blob/c21bf496bca87d54e75bad048563c3adb1782081/MCPForUnity/Editor/Helpers/PortManager.cs#L113-L149","documentation":"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.","triggerScenarios":"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.","commonSituations":"Many local dev servers/listeners running; ephemeral-port exhaustion; OS/firewall reserving a wide range; MaxPortAttempts too small for the workload.","solutions":["Free up ports within the scanned range by stopping unneeded listeners.","Raise MaxPortAttempts or change DefaultPort in configuration if the value is configurable.","Reduce the number of concurrent Unity/Server instances competing for ports."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// Count free ports in the candidate window before relying on FindAvailablePort.\nint free = Enumerable.Range(PortManager.DefaultPort + 1, PortManager.MaxPortAttempts - 1)\n    .Count(PortManager.IsPortAvailable);\nif (free == 0)\n    throw new InvalidOperationException(\"No free ports in the scan window; reduce running listeners.\");","typeGuard":null,"tryCatchPattern":"try { return PortManager.FindAvailablePort(); }\ncatch (Exception ex) when (ex.Message.Contains(\"No available ports\"))\n{\n    // Surface actionable guidance: free ports or widen the window.\n    throw new InvalidOperationException(\"Port range exhausted; stop other listeners or extend MaxPortAttempts.\", ex);\n}","preventionTips":["Limit the number of concurrent local listeners competing for ports.","Widen MaxPortAttempts or move DefaultPort if the range is frequently exhausted.","Free orphaned processes that hold ports after crashes."],"tags":["port","network","resource-exhaustion"],"backgroundTag":null,"analyzedSha":"c21bf496bca87d54e75bad048563c3adb1782081","analyzedAt":"2026-08-13T17:36:56.095Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}