question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Potential leak of `AutofacServiceProvider`

See original GitHub issue

Hi! This might be a bit of a vague question, so apologies in advance if I’m completely off-track.

I’ve been working on leak detection via Autofac.Analysis, and discovered that when ASP.NET Core 2.1 registers the service provider, it doesn’t appear to mark it as a singleton. At runtime the AutofacServiceProvider instance that’s returned is disposable, so AFAIK it’ll end up in the Disposer’s stack (there may still be only one instance, but entries in the stack will grow).

image

It doesn’t look like there’s any common path in ASP.NET Core that triggers the leak - but it could be a bit of a footgun. Curious whether there’s anything I’m missing here…? Using 4.9 and the latest version of this package. Thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
tilligcommented, Feb 21, 2019

Yeah, I’m not sure. I really tried reverse engineering how the framework wants to get a reference to the provider vs. what you would expect to do in a per-request/per-scope service location thing and… the hosting mechanism over there now is so complex I just got lost. (Though, admittedly, some of the stuff they do with different DI behaviors, like ILEmit vs. dynamic, etc. is sort of interesting.)

I do know that the framework thinks it’s responsible for disposing the root provider… but the consumers of IServiceProvider assume that the provider will be disposed for them, whether it’s at the root level or in a request.

ExternallyOwned may be the way to get around it. Just stop Autofac from tracking it and let other systems - the framework, the user, the service scope - handle the disposal manually. That would at least be reasonably consistent with how lifetime scopes work - you create it, we assume you dispose it. No tracking, no automatic disposal.

0reactions
nblumhardtcommented, Feb 21, 2019

👍 the IServiceProvider/ILifetimeScope analogy is pretty good (just very strange that the framework would then expect unique instances, but 🤷‍♂️ ).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to avoid memory leaks with Autofac?
The official guidance is there to help you prevent inadvertent memory leaks. Since Autofac holds references to disposable things so that it ...
Read more >
Find, Fix, and Avoid Memory Leaks in C# .NET: 8 Best ...
Finding, Fixing and learning to Avoid Memory Leaks is an important skill. I'll list 8 best practice techniques used by me and senior...
Read more >
Detecting and Preventing Memory Leaks in .NET
Memory leakage happens when an application or program holds a computer's primary memory for a prolonged time. You can think of it as...
Read more >
Fixing a memory leak in .Net Core - A Method to Madness
How do you investigate and fix memory leaks in .Net Core ? Where do you start ? Here is a real-life example on...
Read more >
Memory leak : r/csharp
Memory leak. I made an application that runs 24/7 on a server. It's constantly deserializing JSON files that are produce by some machines ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found