Skip to content

Commit 09c9efd

Browse files
fix: cancellationTokenSource memory leak (#50958)
* fix: cancellationTokenSource memory leak I've added 2 fixes to prevent memory leak: - using statement to CancellationToken Source - dispose to CancellationTokenRegistration Found by Linux Verification Center (linuxtesting.org) with SVACE. * fix: compilation error
1 parent 3f7e708 commit 09c9efd

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/Components/WebAssembly/Server/src/DebugProxyLauncher.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ private static async Task<string> LaunchAndGetUrl(IServiceProvider serviceProvid
7171
};
7272
RemoveUnwantedEnvironmentVariables(processStartInfo.Environment);
7373

74+
using var cts = new CancellationTokenSource(DebugProxyLaunchTimeout);
75+
var ctr = default(CancellationTokenRegistration);
7476
var debugProxyProcess = Process.Start(processStartInfo);
7577
if (debugProxyProcess is null)
7678
{
@@ -81,13 +83,20 @@ private static async Task<string> LaunchAndGetUrl(IServiceProvider serviceProvid
8183
PassThroughConsoleOutput(debugProxyProcess);
8284
CompleteTaskWhenServerIsReady(debugProxyProcess, isFirefox, tcs);
8385

84-
new CancellationTokenSource(DebugProxyLaunchTimeout).Token.Register(() =>
86+
ctr = cts.Token.Register(() =>
8587
{
8688
tcs.TrySetException(new TimeoutException($"Failed to start the debug proxy within the timeout period of {DebugProxyLaunchTimeout.TotalSeconds} seconds."));
8789
});
8890
}
8991

90-
return await tcs.Task;
92+
try
93+
{
94+
return await tcs.Task;
95+
}
96+
finally
97+
{
98+
ctr.Dispose();
99+
}
91100
}
92101

93102
private static void RemoveUnwantedEnvironmentVariables(IDictionary<string, string?> environment)

0 commit comments

Comments
 (0)