runtime: Circular reference results in deadlock

In 3.0 we rewrote the code generation logic in the DI container to make it resillent to stack overflows for deep object graphs. As a result of this change, what would previously result in a stackoverflow exception for lazily resolved circular references now results in an infinite recursion. We’re missing a number of stacks maximum. a deadlock.

using System;
using Microsoft.Extensions.DependencyInjection;

namespace WebApplication315
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var services = new ServiceCollection();
            services.AddSingleton<Foo>();
            var serviceProvider = services.BuildServiceProvider();
            var foo = serviceProvider.GetService<Foo>();
        }
    }

    public class Foo
    {
        public Foo(IServiceProvider serviceProvider)
        {
            serviceProvider.GetService<Foo>();
        }
    }
}

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 12
  • Comments: 21 (9 by maintainers)

Most upvoted comments

No this isn’t fixed.

How should we proceed with implementing the fix?

Is there a work around to detect this during run time? We are working on a big code base that is slowly moving to dependency containers and I can already see this coming back to bite us many times…

I believe I also just fell in this hole, on net6.0. Took quite some digging around to figure out that a circular reference was the culprit, I was certain we’d get an exception from the DI container if that happened and didn’t even look at that when I started my investigation 🙈

I have just come across this issue so it still needs looking at.