{"record":{"id":"3124007f2e4ecf41","repo":"CoplayDev/unity-mcp","slug":"port-must-be-positive","errorCode":null,"errorMessage":"Port must be positive.","messagePattern":"Port must be positive\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MCPForUnity/Editor/Helpers/PortManager.cs","lineNumber":94,"sourceCode":"\n        /// <summary>\n        /// Decide whether a configured port that keeps reporting AddressAlreadyInUse should be\n        /// abandoned for a freshly discovered port. Returns false (keep retrying the same port)\n        /// until it has been continuously busy for <see cref=\"BusyPortFallbackWindowSeconds\"/>,\n        /// which distinguishes a domain-reload socket-release race from a foreign occupant (#1173).\n        /// </summary>\n        public static bool ShouldAbandonBusyPort(double busyForSeconds)\n            => busyForSeconds >= BusyPortFallbackWindowSeconds;\n\n        /// <summary>\n        /// Persist a user-selected port and return the value actually stored.\n        /// If <paramref name=\"port\"/> is unavailable, the next available port is chosen instead.\n        /// </summary>\n        public static int SetPreferredPort(int port)\n        {\n            if (port <= 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(port), \"Port must be positive.\");\n            }\n\n            if (!IsPortAvailable(port))\n            {\n                throw new InvalidOperationException($\"Port {port} is already in use.\");\n            }\n\n            SavePort(port);\n            return port;\n        }\n\n        /// <summary>\n        /// Find an available port starting from the default port\n        /// </summary>\n        /// <returns>Available port number</returns>\n        private static int FindAvailablePort()\n        {\n            // Always try default port first","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/CoplayDev/unity-mcp/blob/c21bf496bca87d54e75bad048563c3adb1782081/MCPForUnity/Editor/Helpers/PortManager.cs#L76-L112","documentation":"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.","triggerScenarios":"Calling SetPreferredPort with 0, a negative number, or an uninitialized int (default 0) from missing config. The check is 'if (port <= 0)'.","commonSituations":"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.","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)."],"exampleFix":"// before\nSetPreferredPort(parsedPort); // parsedPort may be 0 when config missing\n\n// after\nint port = parsedPort > 0 ? parsedPort : PortManager.DefaultPort;\nSetPreferredPort(port);","handlingStrategy":"validation","validationCode":"int NormalizePort(int candidate, int fallback)\n{\n    if (candidate <= 0 || candidate > 65535) return fallback;\n    return candidate;\n}\n\nint port = NormalizePort(configPort, PortManager.DefaultPort);\nif (port <= 0) throw new InvalidOperationException(\"No valid port configured.\");\nPortManager.SetPreferredPort(port);","typeGuard":"static bool IsValidPort(int port) => port > 0 && port <= 65535;","tryCatchPattern":"try { PortManager.SetPreferredPort(port); }\ncatch (ArgumentOutOfRangeException)\n{\n    // Config supplied an invalid port; fall back to the default and retry.\n    PortManager.SetPreferredPort(PortManager.DefaultPort);\n}","preventionTips":["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."],"tags":["port","network","validation","argument"],"backgroundTag":null,"analyzedSha":"c21bf496bca87d54e75bad048563c3adb1782081","analyzedAt":"2026-08-13T17:36:56.095Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}