From d52064744498e31c36cb7c7088ae88e0cfd88265 Mon Sep 17 00:00:00 2001 From: waldekmastykarz Date: Sun, 22 Nov 2026 10:49:10 +0100 Subject: [PATCH 1/2] Reject out-of-range port and apiPort from config file. Closes #2140 An out-of-range port or apiPort in the config file crashed Dev Proxy with an unhandled ArgumentOutOfRangeException from Kestrel. Validate both values at startup and report a clear error naming the setting and the valid range (0-65535) with exit code 1, like other config errors. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- DevProxy.Tests/DevProxyCommandTests.cs | 23 +++++++++++++++++++++++ DevProxy/Commands/DevProxyCommand.cs | 21 +++++++++++++++++++++ DevProxy/Program.cs | 7 ++++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/DevProxy.Tests/DevProxyCommandTests.cs b/DevProxy.Tests/DevProxyCommandTests.cs index ffcba30f..a4ca1517 100644 --- a/DevProxy.Tests/DevProxyCommandTests.cs +++ b/DevProxy.Tests/DevProxyCommandTests.cs @@ -51,4 +51,27 @@ public void ApplyDiscoverOption_False_KeepsRecordingEnabledFromOtherSources() Assert.True(config.Record); } + + [Theory] + [InlineData(-1, 8897, "port")] + [InlineData(70000, 8897, "port")] + [InlineData(8000, -1, "apiPort")] + [InlineData(8000, 70000, "apiPort")] + public void GetInvalidConfigPortMessage_OutOfRange_NamesSettingAndRange(int port, int apiPort, string setting) + { + var message = DevProxyCommand.GetInvalidConfigPortMessage(port, apiPort); + + Assert.NotNull(message); + Assert.Contains($"'{setting}'", message, StringComparison.Ordinal); + Assert.Contains("between 0 and 65535", message, StringComparison.Ordinal); + } + + [Theory] + [InlineData(0, 0)] + [InlineData(8000, 8897)] + [InlineData(65535, 65535)] + public void GetInvalidConfigPortMessage_InRange_ReturnsNull(int port, int apiPort) + { + Assert.Null(DevProxyCommand.GetInvalidConfigPortMessage(port, apiPort)); + } } diff --git a/DevProxy/Commands/DevProxyCommand.cs b/DevProxy/Commands/DevProxyCommand.cs index e46dbe45..c07adfaa 100644 --- a/DevProxy/Commands/DevProxyCommand.cs +++ b/DevProxy/Commands/DevProxyCommand.cs @@ -288,6 +288,20 @@ internal static void ValidatePort(OptionResult input) } } + internal static bool IsValidPort(int port) => port is >= 0 and <= System.Net.IPEndPoint.MaxPort; + + // CLI port options are validated by ValidatePort, so an out-of-range + // value here comes from the configuration file. + internal static string? GetInvalidConfigPortMessage(int port, int apiPort) + { + var (setting, value) = !IsValidPort(port) ? ("port", port) + : !IsValidPort(apiPort) ? ("apiPort", apiPort) + : (null, 0); + return setting is null + ? null + : $"'{value}' is not a valid value for '{setting}' in the configuration file. Specify a number between 0 and {System.Net.IPEndPoint.MaxPort}."; + } + private async Task InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken) { if (_app is null) @@ -314,6 +328,13 @@ private async Task InvokeAsync(ParseResult parseResult, CancellationToken c ConfigureFromOptions(parseResult); + var invalidPort = GetInvalidConfigPortMessage(_proxyConfiguration.Port, _proxyConfiguration.ApiPort); + if (invalidPort is not null) + { + _logger.LogError("{InvalidPort}", invalidPort); + return 1; + } + var portConflict = await PortConflict.CheckAsync(PortKind.Proxy, _proxyConfiguration.IPAddress, _proxyConfiguration.Port, cancellationToken) ?? await PortConflict.CheckAsync(PortKind.Api, _proxyConfiguration.ApiIpAddress, _proxyConfiguration.ApiPort, cancellationToken); diff --git a/DevProxy/Program.cs b/DevProxy/Program.cs index 7a07a4c8..177e54c7 100644 --- a/DevProxy/Program.cs +++ b/DevProxy/Program.cs @@ -468,7 +468,12 @@ static WebApplication BuildApplicationCore( } _ = builder.WebHost.ConfigureKestrel(options => { - options.Listen(apiAddress, apiPort); + // An out-of-range port would throw while building the app. Skip it so + // DevProxyCommand can report it as a configuration error instead. + if (DevProxyCommand.IsValidPort(apiPort)) + { + options.Listen(apiAddress, apiPort); + } }); var app = builder.Build(); From 858433de40cd5c184591a4226973427856de0bc9 Mon Sep 17 00:00:00 2001 From: Waldek Mastykarz Date: Thu, 26 Nov 2026 16:21:11 +0100 Subject: [PATCH 2/2] Retry temp dir cleanup in process smoke tests on Windows Killed child processes (e.g. cmd.exe /c more in the stdio test) release handles on their working directory asynchronously on Windows, so the immediate recursive delete intermittently failed with IOException. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../ProcessSmokeTests.cs | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/DevProxy.Integration.Tests/ProcessSmokeTests.cs b/DevProxy.Integration.Tests/ProcessSmokeTests.cs index c89f2d52..e8ab25f7 100644 --- a/DevProxy.Integration.Tests/ProcessSmokeTests.cs +++ b/DevProxy.Integration.Tests/ProcessSmokeTests.cs @@ -521,7 +521,7 @@ public async Task Host_LoadsMockPluginFromConfig_AndServesMock() } finally { - workDir.Delete(recursive: true); + await DeleteDirectoryAsync(workDir); // The host writes a timestamped log to the working dir's parent in some modes; // the temp dir delete above covers logs written under workDir. Best-effort. } @@ -591,7 +591,7 @@ await process.StandardInput.WriteAsync( } finally { - workDir.Delete(recursive: true); + await DeleteDirectoryAsync(workDir); } } @@ -650,7 +650,7 @@ await File.WriteAllTextAsync(Path.Combine(workDir.FullName, "devproxyrc.json"), } finally { - workDir.Delete(recursive: true); + await DeleteDirectoryAsync(workDir); } } @@ -735,6 +735,30 @@ private static async Task WaitForListeningPortAsync(Process process) } } + /// + /// Deletes a temp working directory, retrying while Windows releases handles held by + /// just-killed processes (e.g. a child whose current directory is ). + /// + private static async Task DeleteDirectoryAsync(DirectoryInfo dir) + { + for (var attempt = 1; ; attempt++) + { + try + { + dir.Refresh(); + if (dir.Exists) + { + dir.Delete(recursive: true); + } + return; + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException && attempt < 20) + { + await Task.Delay(250); + } + } + } + private static void StopHost(Process process) { try