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
InvalidOperationException from Owned<T> lifetime scope circular dependency #927
Comments
It looks like you have a circular reference. Here's how I'm reading it:
Regardless of whether they're The overall change you're seeing is due to #882 - you really shouldn't be nesting lifetime scopes that are tagged with the same name. Like, you can't have a web request scope inside another web request scope. When Just brainstorming, I think we could possibly modify this such that the tag on the Off hand, I can't think of any reason the @alexmg Can you think of anything I'm missing with that? |
Now that I'm looking at this, no matter how I slice it the repro always ends up with a circular reference in it. Any hierarchy I create that has the same
Like if Is there a repro of this that doesn't have a circular dependency chain? If not, I'm feeling more like this is a "don't do that" situation we could cover in documentation - |
It's not a normal circular dependency like
In the example I should probably use interface instead of concrete type (result is the same anyway). Using interface is how people break the circular dependency. The code works totally fine without
|
It does seem like there is a circular dependency waiting to happen. I noticed that the |
I'm fairly sure that using something like The question is whether or not the situation is considered too dangerous allow. That circular dependency caused by the |
By and large Autofac has been pretty laissez-faire about allowing people to do things they want to do when it comes to advanced scenarios without putting up too many guard rails. Like the "parent scope disposal doesn't dispose child lifetime scopes" thing. You set it up, you get to dispose it. Which isn't to say a StackOverflowException from the circular dependency will be better, but it is what it is. Let me do a quick check to see what happens in this situation if we uniquely name the scopes. |
I switched the public void Execute()
{
using (var bar = this._barFactory())
{
// Execute isn't getting called so the circle doesn't quite complete... yet.
}
} As soon as you complete the circle, it hits public void Execute()
{
using (var bar = this._barFactory())
{
bar.Value.Execute();
}
} I recognize in reality that For reference, if you switch the public class Bar
{
private readonly Func<Zzz> _zzzFactory;
public Bar(Func<Zzz> zzzFactory)
{
this._zzzFactory = zzzFactory;
}
public void Execute()
{
var zzz = this._zzzFactory();
zzz.Execute();
}
}
public class Foo
{
private readonly Func<Bar> _barFactory;
public Foo(Func<Bar> barFactory)
{
this._barFactory = barFactory;
}
public void Execute()
{
var bar = this._barFactory();
bar.Execute();
}
}
public class Zzz
{
private readonly Func<Bar> _barFactory;
public Zzz(Func<Bar> barFactory)
{
this._barFactory = barFactory;
}
public void Execute()
{
var bar = this._barFactory();
// It works if you don't complete the circle.
// Put this in to make the StackOverflowException happen.
// bar.Execute();
}
} I guess that leaves us a couple of options:
I'm currently in favor of option 1- stop the circle from happening and make the exception more clear. |
when using v3, there was no error calling the bar.execute, I didn't add that line just because already able to reproduce the exception in v4. |
The last Autofac 3 release was 2014. A lot has changed, both from an API/features perspective and from an internals perspective. Either way, if bar.Execute gets called, at this point it will invoke the whole resolution chain all the way down forever yielding a stack overflow. If it didn't throw in v3, that sounds like a bug in v3, not a feature to add in v4. |
I'm updating the exception message to:
Hopefully that will at least make this less confusing until we can improve circular dependency detection. |
We recently upgraded to Autofac 4.8.1 from 3.x and noticed this issue.
Stacktrace:
Steps to reproduce:
Admittedly, the code is smelly. But just let you know something is broken if not intentionally.
The text was updated successfully, but these errors were encountered: