Skip to content

Memory leak in System.DirectoryServices.Protocols #40946

@uiopak

Description

@uiopak

Description

Calling LdapConnection.SendRequest method create memory leak.
Making request that returns 14500 entries in loop 100 times allocates 1.5 GB in native heap that later isn't deallocated in 5.0.0-preview.4.20251.6 version and earlier it is 7MB. When creating 100 000 users it is 96MB and 7MB.

my code

private static string dn = "CN=Users,DC=test,DC=local";
static void Main(string[] args)
{
    string userName = "[email protected]";
    string password = "Password1!";
    string ip = "192.168.1.182";
    int port = 389;
    LdapDirectoryIdentifier ldi = new LdapDirectoryIdentifier(ip, port);
    LdapConnection connection = new LdapConnection(ldi);

    NetworkCredential credential = new NetworkCredential(userName, password);

    connection.Credential = credential;
    connection.AuthType = AuthType.Basic;

    LdapSessionOptions options = connection.SessionOptions;
    options.ProtocolVersion = 3;
    try
    {
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
        {
            options.VerifyServerCertificate
            += (conn, cert) => { return true; };
        }
        options.StartTransportLayerSecurity(null);
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        return;
    }

    try
    {
        connection.Bind();
    }
    catch (LdapException e)
    {
        Console.WriteLine(e);
        return;
    }

    for (int i = 0; i < 100; i++)
    {
        try
        {
            string distinguishedName = $"CN=Name{i},{dn}";
            AddRequest addRequest = new AddRequest(distinguishedName, "user");
            switch (0)
            {
                case 0:
                    returnMaxID(connection);
                    break;
                case 1:
                    addRequest.Attributes.Add(new DirectoryAttribute("uidNumber", (returnMaxID(connection) + 1).ToString()));
                    goto case 2;
                case 2:
                    {
                        addRequest.Attributes.Add(new DirectoryAttribute("unicodePwd", Encoding.Unicode.GetBytes("\"Password1!\"")));
                        var response = connection.SendRequest(addRequest);
                    }
                    break;
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

    try
    {
        options.StopTransportLayerSecurity();
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }

    connection.Dispose();
}
private static int returnMaxID(LdapConnection connection)
{
    var request = new SearchRequest(dn, $"(&(objectClass=user)(uidNumber=*))",
        SearchScope.Subtree, new[] { "uidNumber" });
    var pageResultRequestControl = new PageResultRequestControl(10000);
    request.Controls.Add(pageResultRequestControl);
    try
    {
        List<SearchResultEntry> searchResultEntries = new List<SearchResultEntry>();
        while (true)
        {
            var response = connection.SendRequest(request) as SearchResponse;
            if (response.Entries.Count > 0)
            {
                SearchResultEntry[] sre = new SearchResultEntry[response.Entries.Count];
                response.Entries.CopyTo(sre, 0);
                searchResultEntries.AddRange(sre);
            }
            pageResultRequestControl.Cookie = (response.Controls[0] as PageResultResponseControl).Cookie;
            if (pageResultRequestControl.Cookie.Length == 0)
            {
                break;
            }
        }

        int max = 9999;
        if (searchResultEntries.Count > 0)
        {
            max = searchResultEntries.Max(e => int.Parse(e.Attributes["uidNumber"][0].ToString()));
        }
        return max;
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }
    return 9999;
}

Configuration

Windows 10 Pro 2004 x64
.Net Core 3.1.6 and 5.0.0-preview.7.20364.11
Linux Manjaro
.Net Core 3.1.3
Debian 10(Docker 3.1-buster-slim)
.Net Core 3.1.7

Regression?

Problem started with 5.0.0-preview.5.20278.1 version of library.

Other information

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions