Skip to content

Commit

Permalink
- Fixes race with captive scoped services
Browse files Browse the repository at this point in the history
Fixes: #52221
  • Loading branch information
maryamariyan committed May 27, 2021
1 parent bc0fda4 commit 556768a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ public ILEmitResolverBuilder(ServiceProvider serviceProvider)

public Func<ServiceProviderEngineScope, object> Build(ServiceCallSite callSite)
{
return BuildType(callSite).Lambda;
Func<ServiceProviderEngineScope, object> lambda = BuildType(callSite).Lambda;

return (scope) => scope.IsRootScope ?
_runtimeResolver.Resolve(callSite, scope) : lambda(scope);
}

private GeneratedMethod BuildType(ServiceCallSite callSite)
Expand Down Expand Up @@ -99,7 +102,7 @@ private GeneratedMethod BuildTypeNoCache(ServiceCallSite callSite)
"ResolveService", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(object),
new[] { typeof(ILEmitResolverBuilderRuntimeContext), typeof(ServiceProviderEngineScope) });

GenerateMethodBody(callSite, method.GetILGenerator(), info);
GenerateMethodBody(callSite, method.GetILGenerator());
type.CreateTypeInfo();
assembly.Save(assemblyName + ".dll");
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,45 @@ public void ScopedServiceResolvedFromSingletonAfterCompilation()
Thread.Sleep(10); // Give the background thread time to compile
}
}

[Fact]
public void ScopedServiceResolvedFromSingletonAfterCompilation2()
{
ServiceProvider sp = new ServiceCollection()
.AddScoped<A>()
.AddSingleton<IFakeOpenGenericService<A>, FakeOpenGenericService<A>>()
.BuildServiceProvider();

var scope = sp.CreateScope();
for (int i = 0; i < 50; i++)
{
scope.ServiceProvider.GetRequiredService<A>();
Thread.Sleep(10); // Give the background thread time to compile
}

Assert.Same(sp.GetRequiredService<IFakeOpenGenericService<A>>().Value, sp.GetRequiredService<A>());
}

[Fact]
public void ScopedServiceResolvedFromSingletonAfterCompilation3()
{
// Singleton IFakeX<A> -> Scoped A -> Scoped Aa
ServiceProvider sp = new ServiceCollection()
.AddScoped<Aa>()
.AddScoped<A>()
.AddSingleton<IFakeOpenGenericService<Aa>, FakeOpenGenericService<Aa>>()
.BuildServiceProvider();

var scope = sp.CreateScope();
for (int i = 0; i < 50; i++)
{
Assert.Same(sp.GetRequiredService<IFakeOpenGenericService<Aa>>().Value.PropertyA, sp.GetRequiredService<A>());
Thread.Sleep(10); // Give the background thread time to compile
}

Assert.Same(sp.GetRequiredService<IFakeOpenGenericService<Aa>>().Value.PropertyA, sp.GetRequiredService<A>());
}


private async Task<bool> ResolveUniqueServicesConcurrently()
{
Expand Down Expand Up @@ -1150,5 +1189,13 @@ private class G { }
private class H { }
private class I { }
private class J { }
private class Aa
{
public Aa(A a)
{
PropertyA = a;
}
public A PropertyA { get; }
}
}
}

0 comments on commit 556768a

Please sign in to comment.