From 814daeaca21cefc56856a53acdbc2acc3c3c7229 Mon Sep 17 00:00:00 2001 From: waldekmastykarz Date: Sat, 4 Jul 2026 15:54:08 +0200 Subject: [PATCH] Fixes hot reload --- DevProxy/Program.cs | 23 +++-------------------- DevProxy/Proxy/ConfigFileWatcher.cs | 18 +----------------- 2 files changed, 4 insertions(+), 37 deletions(-) diff --git a/DevProxy/Program.cs b/DevProxy/Program.cs index fc39b88f1..caa4b8af9 100644 --- a/DevProxy/Program.cs +++ b/DevProxy/Program.cs @@ -406,28 +406,11 @@ static async Task RunProxyAsync(string[] args, DevProxyConfigOptions option { // Reset the restart flag before each run ConfigFileWatcher.Reset(); + // RunProxyAsync only returns after the host has fully stopped, which + // means the proxy engine's shutdown (including system proxy + // deregistration) has completed — so it's safe to start a new instance. exitCode = await RunProxyAsync(args, options); - // Wait for proxy to fully stop (including system proxy deregistration) - // before starting the new instance - if (ConfigFileWatcher.ProxyStoppedCompletionSource is not null) - { - var proxyStoppedTask = ConfigFileWatcher.ProxyStoppedCompletionSource.Task; - var timeoutTask = Task.Delay(TimeSpan.FromSeconds(30)); -#pragma warning disable VSTHRD003 // Intentionally waiting for external signal - var completedTask = await Task.WhenAny(proxyStoppedTask, timeoutTask); -#pragma warning restore VSTHRD003 - - // If the timeout elapses before the proxy signals it has stopped, - // continue to avoid hanging the restart loop indefinitely - if (completedTask == proxyStoppedTask) - { -#pragma warning disable VSTHRD003 // Observe exceptions from completed task - await proxyStoppedTask; -#pragma warning restore VSTHRD003 - } - } - shouldRestart = ConfigFileWatcher.IsRestarting; } catch (Exception ex) diff --git a/DevProxy/Proxy/ConfigFileWatcher.cs b/DevProxy/Proxy/ConfigFileWatcher.cs index 261ff4d4a..0946307ec 100644 --- a/DevProxy/Proxy/ConfigFileWatcher.cs +++ b/DevProxy/Proxy/ConfigFileWatcher.cs @@ -23,24 +23,10 @@ sealed class ConfigFileWatcher( /// public static bool IsRestarting { get; private set; } - /// - /// Signaled when the proxy has fully stopped and system proxy is deregistered. - /// - public static TaskCompletionSource? ProxyStoppedCompletionSource { get; private set; } - /// /// Resets the restart flag. Called before each proxy run. /// - public static void Reset() - { - IsRestarting = false; - ProxyStoppedCompletionSource = null; - } - - /// - /// Signals that the proxy has fully stopped. - /// - public static void SignalProxyStopped() => ProxyStoppedCompletionSource?.TrySetResult(); + public static void Reset() => IsRestarting = false; public Task StartAsync(CancellationToken cancellationToken) { @@ -93,14 +79,12 @@ private void OnConfigFileChanged(object sender, FileSystemEventArgs e) { _logger.LogInformation("Configuration file changed. Restarting proxy..."); IsRestarting = true; - ProxyStoppedCompletionSource = new TaskCompletionSource(); _hostApplicationLifetime.StopApplication(); } catch (Exception ex) { _logger.LogError(ex, "Error while handling configuration file change. Restart has not been initiated."); IsRestarting = false; - ProxyStoppedCompletionSource = null; } }