Skip to content

Commit c446f34

Browse files
jacobtomlinsonCopilotshanselman
authored
fix: prevent .NET 10 COM wrapper memory leak in production (#1215)
* fix: disable .NET COM wrapper diagnostics in production Work around dotnet/runtime#129191 for Release production builds while preserving managed debugger support in Debug and explicit DevBuild builds. Co-authored-by: Copilot <[email protected]> * test: verify COM wrapper runtime switch matrix Co-authored-by: Copilot App <[email protected]> --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: Scott Hanselman <[email protected]>
1 parent fd8901d commit c446f34

2 files changed

Lines changed: 111 additions & 0 deletions

File tree

‎src/OpenClaw.Tray.WinUI/OpenClaw.Tray.WinUI.csproj‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
<AppIdentityMarker>release</AppIdentityMarker>
3333
</PropertyGroup>
3434

35+
<!-- Work around dotnet/runtime#129191 in production builds. Explicit DevBuild
36+
builds retain debugger support for managed/native interop diagnostics. -->
37+
<ItemGroup Condition="'$(Configuration)' == 'Release' and '$(DevBuild)' != 'true'">
38+
<RuntimeHostConfigurationOption Include="System.Diagnostics.Debugger.IsSupported"
39+
Value="false"
40+
Trim="true" />
41+
</ItemGroup>
42+
3543
<!-- Map $(Platform) to $(RuntimeIdentifier) so self-contained WinUI F5/debug produces a launchable EXE. -->
3644
<PropertyGroup Condition="'$(RuntimeIdentifier)' == '' and '$(Platform)' == 'x64'">
3745
<RuntimeIdentifier>win-x64</RuntimeIdentifier>

‎tests/OpenClaw.Tray.Tests/InstallerIssAssertionTests.cs‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Diagnostics;
2+
13
namespace OpenClaw.Tray.Tests;
24

35
/// <summary>
@@ -198,6 +200,107 @@ public void LocalInstallerBuild_UsesOneIdentitySwitchAndValidatesPayloadMarker()
198200
Assert.DoesNotContain("<DevBuild>true</DevBuild>", project);
199201
}
200202

203+
[Theory]
204+
[InlineData("Release", false, false, "win-x64", true)]
205+
[InlineData("Release", false, true, "win-x64", true)]
206+
[InlineData("Release", false, false, "win-arm64", true)]
207+
[InlineData("Release", false, true, "win-arm64", true)]
208+
[InlineData("Release", true, false, "win-x64", false)]
209+
[InlineData("Debug", false, false, "win-x64", false)]
210+
public async Task ComWrapperDiagnosticsSwitch_EvaluatesOnlyForProductionBuilds(
211+
string configuration,
212+
bool devBuild,
213+
bool packageMsix,
214+
string runtimeIdentifier,
215+
bool expected)
216+
{
217+
var repositoryRoot = TestRepositoryPaths.GetRepositoryRoot();
218+
var projectPath = Path.Combine(
219+
repositoryRoot,
220+
"src",
221+
"OpenClaw.Tray.WinUI",
222+
"OpenClaw.Tray.WinUI.csproj");
223+
var startInfo = new ProcessStartInfo("dotnet")
224+
{
225+
WorkingDirectory = repositoryRoot,
226+
RedirectStandardOutput = true,
227+
RedirectStandardError = true,
228+
UseShellExecute = false,
229+
CreateNoWindow = true,
230+
};
231+
startInfo.ArgumentList.Add("msbuild");
232+
startInfo.ArgumentList.Add(projectPath);
233+
startInfo.ArgumentList.Add("-nologo");
234+
startInfo.ArgumentList.Add("-v:q");
235+
startInfo.ArgumentList.Add("-getItem:RuntimeHostConfigurationOption");
236+
startInfo.ArgumentList.Add($"-p:Configuration={configuration}");
237+
startInfo.ArgumentList.Add($"-p:DevBuild={devBuild.ToString().ToLowerInvariant()}");
238+
startInfo.ArgumentList.Add($"-p:PackageMsix={packageMsix.ToString().ToLowerInvariant()}");
239+
startInfo.ArgumentList.Add($"-p:RuntimeIdentifier={runtimeIdentifier}");
240+
241+
using var process = Process.Start(startInfo);
242+
Assert.NotNull(process);
243+
var standardOutput = process.StandardOutput.ReadToEndAsync();
244+
var standardError = process.StandardError.ReadToEndAsync();
245+
using var timeout = new CancellationTokenSource(TimeSpan.FromMinutes(2));
246+
try
247+
{
248+
await process.WaitForExitAsync(timeout.Token);
249+
}
250+
catch (OperationCanceledException) when (timeout.IsCancellationRequested)
251+
{
252+
if (!process.HasExited)
253+
process.Kill(entireProcessTree: true);
254+
await process.WaitForExitAsync();
255+
256+
throw new TimeoutException(
257+
$"MSBuild evaluation timed out.{Environment.NewLine}" +
258+
$"Standard output:{Environment.NewLine}{await standardOutput}{Environment.NewLine}" +
259+
$"Standard error:{Environment.NewLine}{await standardError}");
260+
}
261+
262+
var output = await standardOutput;
263+
var error = await standardError;
264+
265+
Assert.True(
266+
process.ExitCode == 0,
267+
$"MSBuild evaluation failed with exit code {process.ExitCode}.{Environment.NewLine}" +
268+
$"Standard output:{Environment.NewLine}{output}{Environment.NewLine}" +
269+
$"Standard error:{Environment.NewLine}{error}");
270+
271+
JsonNode? result;
272+
try
273+
{
274+
result = JsonNode.Parse(output);
275+
}
276+
catch (System.Text.Json.JsonException exception)
277+
{
278+
throw new InvalidDataException(
279+
$"MSBuild evaluation returned invalid JSON:{Environment.NewLine}{output}",
280+
exception);
281+
}
282+
283+
var options = result?["Items"]?["RuntimeHostConfigurationOption"]?.AsArray();
284+
Assert.NotNull(options);
285+
var matchingOptions = options
286+
.Where(option =>
287+
string.Equals(
288+
option?["Identity"]?.GetValue<string>(),
289+
"System.Diagnostics.Debugger.IsSupported",
290+
StringComparison.Ordinal))
291+
.ToArray();
292+
293+
if (!expected)
294+
{
295+
Assert.Empty(matchingOptions);
296+
return;
297+
}
298+
299+
var matchingOption = Assert.Single(matchingOptions);
300+
Assert.Equal("false", matchingOption?["Value"]?.GetValue<string>());
301+
Assert.Equal("true", matchingOption?["Trim"]?.GetValue<string>());
302+
}
303+
201304
[Fact]
202305
public void MsixManifest_IsGeneratedUnderObjWithoutMutatingTrackedSource()
203306
{

0 commit comments

Comments
 (0)