From 142fa10eafc80b07ecd4fb839121516598e05a1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Mon, 4 Nov 2024 14:10:44 +0100 Subject: [PATCH] Fix hangdump space in dump path (#3994) --- .../HangDumpProcessLifetimeHandler.cs | 10 ++++++++++ .../HangDumpTests.cs | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs b/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs index f1f702c90e..e09eab94e6 100644 --- a/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs +++ b/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs @@ -2,6 +2,9 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Globalization; +#if NETCOREAPP +using System.Runtime.InteropServices; +#endif using Microsoft.Testing.Extensions.Diagnostics.Resources; using Microsoft.Testing.Extensions.HangDump.Serializers; @@ -362,6 +365,13 @@ private async Task TakeDumpAsync() _ => throw ApplicationStateGuard.Unreachable(), }; + // Wrap the dump path into "" when it has space in it, this is a workaround for this runtime issue: https://github.com/dotnet/diagnostics/issues/5020 + // It only affects windows. Otherwise the dump creation fails with: [createdump] The pid argument is no longer supported + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && finalDumpFileName.Contains(' ')) + { + finalDumpFileName = $"\"{finalDumpFileName}\""; + } + diagnosticsClient.WriteDump(dumpType, finalDumpFileName, true); #else MiniDumpWriteDump.MiniDumpTypeOption miniDumpTypeOption = _dumpType.ToLowerInvariant().Trim() switch diff --git a/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/HangDumpTests.cs b/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/HangDumpTests.cs index dac37079fb..c73a9da2a0 100644 --- a/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/HangDumpTests.cs +++ b/test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/HangDumpTests.cs @@ -47,6 +47,24 @@ public async Task HangDump_CustomFileName_CreateDump() Assert.IsTrue(dumpFile is not null, $"Dump file not found '{TargetFrameworks.NetCurrent}'\n{testHostResult}'"); } + public async Task HangDump_PathWithSpaces_CreateDump() + { + string resultDir = Path.Combine(_testAssetFixture.TargetAssetPath, Guid.NewGuid().ToString("N"), TargetFrameworks.NetCurrent.Arguments); + string resultDirectory = Path.Combine(resultDir, "directory with spaces"); + Directory.CreateDirectory(resultDirectory); + var testHost = TestInfrastructure.TestHost.LocateFrom(_testAssetFixture.TargetAssetPath, "HangDump", TargetFrameworks.NetCurrent.Arguments); + TestHostResult testHostResult = await testHost.ExecuteAsync( + $"""--hangdump --hangdump-timeout 8s --hangdump-filename myhungdumpfile_%p.dmp --results-directory "{resultDirectory}" """, + new Dictionary + { + { "SLEEPTIMEMS1", "4000" }, + { "SLEEPTIMEMS2", "20000" }, + }); + testHostResult.AssertExitCodeIs(ExitCodes.TestHostProcessExitedNonGracefully); + string? dumpFile = Directory.GetFiles(resultDirectory, "myhungdumpfile_*.dmp", SearchOption.AllDirectories).SingleOrDefault(); + Assert.IsTrue(dumpFile is not null, $"Dump file not found '{TargetFrameworks.NetCurrent}'\n{testHostResult}'"); + } + [Arguments("Mini")] [Arguments("Heap")] [Arguments("Triage")]