diff --git a/src/mono/wasm/host/WebServerStartup.cs b/src/mono/wasm/host/WebServerStartup.cs index 64bf9e7cccbac5..4025bf928441c2 100644 --- a/src/mono/wasm/host/WebServerStartup.cs +++ b/src/mono/wasm/host/WebServerStartup.cs @@ -38,7 +38,11 @@ public static int StartDebugProxy(string devToolsHost) //on managed code will freeze because it will not be able to continue executing the BrowserDebugProxy to get the locals value var executablePath = Path.Combine(System.AppContext.BaseDirectory, "BrowserDebugHost.dll"); var ownerPid = Environment.ProcessId; - var generateRandomPort = new Random().Next(5000, 5300); + // generate a random port in a given range, skipping the ports blocked by browsers: https://chromestatus.com/feature/5064283639513088 + var generateRandomPort = GetNextRandomExcept(5000..5300, + 5060, // SIP + 5061 // SIPS + ); var processStartInfo = new ProcessStartInfo { FileName = "dotnet" + (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : ""), @@ -52,6 +56,17 @@ public static int StartDebugProxy(string devToolsHost) throw new InvalidOperationException("Unable to start debug proxy process."); } return generateRandomPort; + + static int GetNextRandomExcept(Range range, params int[] except) + { + int current; + do + { + current = Random.Shared.Next(range.Start.Value, range.End.Value); + } while (Array.IndexOf(except, current) > -1); + + return current; + } } public void Configure(IApplicationBuilder app,