diff --git a/src/mono/wasm/debugger/BrowserDebugHost/DebugProxyHost.cs b/src/mono/wasm/debugger/BrowserDebugHost/DebugProxyHost.cs index b97ffe7ef0fc7..9129c61c9c308 100644 --- a/src/mono/wasm/debugger/BrowserDebugHost/DebugProxyHost.cs +++ b/src/mono/wasm/debugger/BrowserDebugHost/DebugProxyHost.cs @@ -24,7 +24,7 @@ public static async Task RunDebugProxyAsync(ProxyOptions options, string[] args, { RunDevToolsProxyAsync(options, args, loggerFactory, token) }; - if (!options.RunningForBlazor) + if (!options.RunningForBlazor || options.IsFirefoxDebugging) tasks.Add(RunFirefoxServerLoopAsync(options, args, loggerFactory, token)); Task completedTask = await Task.WhenAny(tasks); diff --git a/src/mono/wasm/debugger/BrowserDebugProxy/Firefox/FirefoxDebuggerProxy.cs b/src/mono/wasm/debugger/BrowserDebugProxy/Firefox/FirefoxDebuggerProxy.cs index 6824ff3811093..57a590af6badb 100644 --- a/src/mono/wasm/debugger/BrowserDebugProxy/Firefox/FirefoxDebuggerProxy.cs +++ b/src/mono/wasm/debugger/BrowserDebugProxy/Firefox/FirefoxDebuggerProxy.cs @@ -5,7 +5,9 @@ using System; using System.Diagnostics.CodeAnalysis; +using System.Linq; using System.Net; +using System.Net.NetworkInformation; using System.Net.Sockets; using System.Threading; using System.Threading.Tasks; @@ -26,10 +28,17 @@ public static void StartListener(int proxyPort, ILogger logger, int browserPort { if (s_tcpListener is null) { + // If there is an existing listener on @proxyPort, then use a new dynamic port. + // Blazor always tries to open the same port (specified in @proxyPort) to avoid + // creating a lot of remote debugging connections on firefox + if (proxyPort != 0 && IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners().Any(x => x.Port == proxyPort)) + { + proxyPort = 0; + } s_tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), proxyPort); s_tcpListener.Start(); Console.WriteLine($"Debug proxy for firefox now listening on tcp://{s_tcpListener.LocalEndpoint}." + - (browserPort >= 0 ? $" And expecting firefox at port {browserPort} ." : string.Empty)); + (browserPort >= 0 ? $" And expecting firefox at port {browserPort}." : string.Empty)); } } diff --git a/src/mono/wasm/debugger/BrowserDebugProxy/ProxyOptions.cs b/src/mono/wasm/debugger/BrowserDebugProxy/ProxyOptions.cs index ca8084795b114..0fdfdfc8a8e01 100644 --- a/src/mono/wasm/debugger/BrowserDebugProxy/ProxyOptions.cs +++ b/src/mono/wasm/debugger/BrowserDebugProxy/ProxyOptions.cs @@ -28,4 +28,5 @@ public int DevToolsDebugPort } public string? LogPath { get; set; } public bool RunningForBlazor { get; set; } + public bool IsFirefoxDebugging { get; set; } }