Skip to content
This repository was archived by the owner on Nov 2, 2018. It is now read-only.
This repository was archived by the owner on Nov 2, 2018. It is now read-only.

Race in ServiceLookup.CallSiteValidator.ValidateCallSite() #467

@mikeharder

Description

@mikeharder

@halter73, @pakrym

I am getting the following exception when running the EF benchmarks (aspnet/Benchmarks#162):

System.ArgumentException: An item with the same key has already been added. Key: Benchmarks.Data.EfDb
   at System.ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException(Object key)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateCallSite(Type serviceType, IServiceCallSite callSite) in C:\Git\DependencyInjection\src\Microsoft.Extensions.DependencyInjection\ServiceLookup\CallSiteValidator.cs:line 19
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType, ServiceProvider serviceProvider) in C:\Git\DependencyInjection\src\Microsoft.Extensions.DependencyInjection\ServiceProvider.cs:line 76
   at System.Collections.Concurrent.ConcurrentDictionaryExtensions.GetOrAdd[TKey,TValue,TArg](ConcurrentDictionary`2 dictionary, TKey key, Func`3 valueFactory, TArg arg) in C:\Git\DependencyInjection\src\Microsoft.Extensions.DependencyInjection\Internal\ConcurrentDictionaryExtensions.cs:line 11
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) in C:\Git\DependencyInjection\src\Microsoft.Extensions.DependencyInjection\ServiceProvider.cs:line 63
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider) in C:\Git\DependencyInjection\src\Microsoft.Extensions.DependencyInjection.Abstractions\ServiceProviderServiceExtensions.cs:line 28
   at Benchmarks.Middleware.MultipleUpdatesEfMiddleware.<Invoke>d__4.MoveNext() in C:\Git\Benchmarks\src\Benchmarks\Middleware\MultipleUpdatesEfMiddleware.cs:line 0

The issue appears to be a race in ServiceLookup.CallSiteValidator.ValidateCallSite():

public void ValidateCallSite(Type serviceType, IServiceCallSite callSite)
{
    var scoped = VisitCallSite(callSite, default(CallSiteValidatorState));
    if (scoped != null)
    {
        _scopedServices.Add(serviceType, scoped);
    }
}

https://github.com/aspnet/DependencyInjection/blob/dev/src/Microsoft.Extensions.DependencyInjection/ServiceLookup/CallSiteValidator.cs#L15-L21

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions