Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak due to caching container properties for SDK hosts which access unbounded number of containers #2471

Closed
johngallardo opened this issue May 15, 2021 · 3 comments · Fixed by #3845
Labels
discussion-wanted Need a discussion on an area feature-request New feature or request

Comments

@johngallardo
Copy link
Member

johngallardo commented May 15, 2021

Describe the bug
An SDK host that accesses a potentially unbounded set of containers experiences a memory leak due to caching ContainerName->ContainerProperty metadata within the SDK.

To Reproduce

            var client = new CosmosClient("<redacted>",
                new CosmosClientOptions
                {
                    ConnectionMode = ConnectionMode.Direct,
                });

            var tasks = Enumerable.Range(0, 16).Select(async _ =>
            {
                while (true)
                {
                    var containerId = $"container_{Guid.NewGuid()}";
                    var container = client.GetContainer("database", containerId);

                    try
                    {
                        using (var feedIterator = container.GetItemLinqQueryable<object>().ToFeedIterator())
                        {
                            while (feedIterator.HasMoreResults) { await feedIterator.ReadNextAsync(); }
                        }
                    }
                    catch (Exception) { }
                }
            }).ToArray();
            await Task.WhenAll(tasks);

Expected behavior
Steady memory consumption within the host process.

Actual behavior
Memory consumption grows unbounded.

Environment summary
SDK Version: 3.13 (although likely repros on newer versions)
OS Version : Windows (dotnetcore 3.1)

Additional context
I have observed this specifically within an SDK host process that handles change feed processing for a multitude of Containers.

@j82w
Copy link
Contributor

j82w commented May 17, 2021

What is the use case for having a single SDK instance to do requests against an unbounded number of containers? I'm not aware of any production use cases for this. Even in the scenario where there are hundreds of containers the overhead of caching the container properties is worth the cost to avoid the additional network latency in the future requests.

@j82w j82w added the discussion-wanted Need a discussion on an area label May 17, 2021
@johngallardo
Copy link
Member Author

johngallardo commented May 17, 2021 via email

@ealsur ealsur added the feature-request New feature or request label May 19, 2021
@johngallardo
Copy link
Member Author

I'm not sure that this is a "Feature-Request."

What I am observing in my service is that collection cache continues to grow unbounded, and additionally the cache entries become polluted with exceptions. In these cases, (where the callback to public async Task<TValue> GetAsync ) throws an exception, it seems that the cache entry goes unused anyways (the code will simply attempt to invoke the provided callback again). In such a case, I wonder if it would be possible to eliminate the entry from the cache all together rather than retain the exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion-wanted Need a discussion on an area feature-request New feature or request
Projects
None yet
4 participants