Skip to content

QuicConnection.ConnectAsync() connection failure causes memory leak. #113030

Closed as not planned
@xlievo

Description

@xlievo

Description

In an automatic reconnection use case, the following code is executed in a timer. If the connection fails, it may cause a memory leak, possibly due to underlying handles not being released.

try
{
    var quicCon = await QuicConnection.ConnectAsync(QuicOptions, cancel.Token);
}
catch
{
    //It will attempt to reconnect again.
}

Reproduction Steps

internal class Program
{
    static async Task Main()
    {
        await Task.Run(async () =>
        {
            if (!QuicConnection.IsSupported)
            {
                throw new PlatformNotSupportedException("QUIC protocol is not supported on this platform.");
            }

            CancellationTokenSource cancel = new();
            using var pingTimer = new PeriodicTimer(TimeSpan.FromSeconds(1));

            while (await pingTimer.WaitForNextTickAsync(cancel.Token))
            {
                try
                {
                    await using var quicCon = await QuicConnection.ConnectAsync(new()
                    {
                        RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, 9999),
                        DefaultCloseErrorCode = 0x0A,
                        DefaultStreamErrorCode = 0x0B,
                        ClientAuthenticationOptions = new SslClientAuthenticationOptions
                        {
                            ApplicationProtocols = [SslApplicationProtocol.Http3],
                            RemoteCertificateValidationCallback = (sender, certificate, chain, errors) => true,
                            TargetHost = "localhost"
                        },
                    }, cancel.Token);

                    break;
                }
                catch
                {
                    //It will attempt to reconnect again.
                }
            }
        });

        Console.WriteLine("exit;");
        Console.ReadLine();
    }
}

Expected behavior

Normal memory.

Actual behavior

The memory will continue to grow without any signs of being reclaimed.

Regression?

No response

Known Workarounds

No response

Configuration

dotnet 9.0 win-x64 AOT publish

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions