Skip to content

SslStream memory usage keeps increasing #115458

Closed
@simpleway2016

Description

@simpleway2016

Description

I used a simple piece of code and found that the memory usage of SslStream keeps increasing when running on Linux.

Reproduction Steps

Server-side code:

    internal class Program
    {
        static X509Certificate2 Cert;
        static async Task Main(string[] args)
        {
            Cert = new X509Certificate2("a.pfx", "123456");
            TcpListener tcpListener = new TcpListener( IPAddress.Any, 8988);
            tcpListener.Start();
            while (true) {
                var socket = await tcpListener.AcceptSocketAsync();
                handle(socket);
            }
        }

        static async void handle(Socket socket)
        {
            try
            {
                using NetworkStream networkStream = new NetworkStream(socket);
                using SslStream sslStream = new SslStream(networkStream);
                await sslStream.AuthenticateAsServerAsync(Cert);
            }
            catch
            {

            }
            finally
            {
                socket.Dispose();
            }
        }

    }

Client-side test code:

        static async Task Main(string[] args)
        {
            SslClientAuthenticationOptions authOptions = new SslClientAuthenticationOptions()
            {
                RemoteCertificateValidationCallback = new RemoteCertificateValidationCallback((a, b, c, d) => true)
            };
            while (true)
            {
                try
                {
                    using TcpClient tcpClient = new TcpClient();
                    await tcpClient.ConnectAsync("127.0.0.1", 8988);
                    using var stream = tcpClient.GetStream();
                    using SslStream sslStream = new SslStream(stream);


                    await sslStream.AuthenticateAsClientAsync(authOptions);
                       
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

Expected behavior

The memory usage of the process should be maintained within a certain fixed range.

Actual behavior

The memory usage of the process keeps increasing continuously.

Regression?

No response

Known Workarounds

No response

Configuration

.net 8.0
Debian 12
x64

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