From a379a89793c78ae9662133a601fd3600f1eeade7 Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Tue, 3 Jun 2025 13:07:54 +0200 Subject: [PATCH 1/2] Launch dsrouter from gcdump Continue the work of #5242 and add a --dsrouter switch to `dotnet-gcdump collect` fixes #5436 --- .../CommandLine/CollectCommandHandler.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Tools/dotnet-gcdump/CommandLine/CollectCommandHandler.cs b/src/Tools/dotnet-gcdump/CommandLine/CollectCommandHandler.cs index 02b0e39412..2ee3a15909 100644 --- a/src/Tools/dotnet-gcdump/CommandLine/CollectCommandHandler.cs +++ b/src/Tools/dotnet-gcdump/CommandLine/CollectCommandHandler.cs @@ -26,6 +26,7 @@ internal static class CollectCommandHandler /// Enable verbose logging. /// The process name to collect the gcdump from. /// The diagnostic IPC channel to collect the gcdump from. + /// The dsrouter command to use for collecting the gcdump. /// private static async Task Collect(CancellationToken ct, int processId, string output, int timeout, bool verbose, string name, string diagnosticPort, string dsrouter) { @@ -80,6 +81,7 @@ private static async Task Collect(CancellationToken ct, int processId, stri if (TryCollectMemoryGraph(ct, processId, diagnosticPort, timeout, verbose, out MemoryGraph memoryGraph)) { GCHeapDump.WriteMemoryGraph(memoryGraph, outputFileInfo.FullName, "dotnet-gcdump"); + DsRouterProcessLauncher.Launcher.Cleanup(); return true; } @@ -87,6 +89,7 @@ private static async Task Collect(CancellationToken ct, int processId, stri }); bool fDumpSuccess = await dumpTask.ConfigureAwait(false); + DsRouterProcessLauncher.Launcher.Cleanup(); if (fDumpSuccess) { @@ -139,7 +142,8 @@ public static Command CollectCommand() VerboseOption, TimeoutOption, NameOption, - DiagnosticPortOption + DiagnosticPortOption, + DsRouterOption }; collectCommand.SetAction(static (parseResult, ct) => Collect(ct, @@ -149,7 +153,7 @@ public static Command CollectCommand() verbose: parseResult.GetValue(VerboseOption), name: parseResult.GetValue(NameOption), diagnosticPort: parseResult.GetValue(DiagnosticPortOption) ?? string.Empty, - dsrouter: string.Empty)); + dsrouter: parseResult.GetValue(DsRouterOption) ?? string.Empty)); return collectCommand; } @@ -191,5 +195,11 @@ public static Command CollectCommand() { Description = "The path to a diagnostic port to collect the dump from." }; + + private static readonly Option DsRouterOption = + new("--dsrouter") + { + Description = "The dsrouter command to use for collecting the gcdump. If specified, the --process-id, --name, or --diagnostic-port options cannot be used." + }; } } From 3d5654582363072e99a11e48225b7f449c9f6638 Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Tue, 3 Jun 2025 22:25:31 +0200 Subject: [PATCH 2/2] avoid code duplication --- .../dotnet-gcdump/CommandLine/CollectCommandHandler.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Tools/dotnet-gcdump/CommandLine/CollectCommandHandler.cs b/src/Tools/dotnet-gcdump/CommandLine/CollectCommandHandler.cs index 2ee3a15909..26af23d866 100644 --- a/src/Tools/dotnet-gcdump/CommandLine/CollectCommandHandler.cs +++ b/src/Tools/dotnet-gcdump/CommandLine/CollectCommandHandler.cs @@ -81,7 +81,6 @@ private static async Task Collect(CancellationToken ct, int processId, stri if (TryCollectMemoryGraph(ct, processId, diagnosticPort, timeout, verbose, out MemoryGraph memoryGraph)) { GCHeapDump.WriteMemoryGraph(memoryGraph, outputFileInfo.FullName, "dotnet-gcdump"); - DsRouterProcessLauncher.Launcher.Cleanup(); return true; } @@ -89,7 +88,6 @@ private static async Task Collect(CancellationToken ct, int processId, stri }); bool fDumpSuccess = await dumpTask.ConfigureAwait(false); - DsRouterProcessLauncher.Launcher.Cleanup(); if (fDumpSuccess) { @@ -113,6 +111,10 @@ private static async Task Collect(CancellationToken ct, int processId, stri Console.Error.WriteLine($"[ERROR] {ex}"); return -1; } + finally + { + DsRouterProcessLauncher.Launcher.Cleanup(); + } } internal static bool TryCollectMemoryGraph(CancellationToken ct, int processId, string diagnosticPort, int timeout, bool verbose, out MemoryGraph memoryGraph)