Skip to content
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

Occational null ref in CreateProxy #653

Closed
HaoK opened this issue Aug 7, 2018 · 2 comments
Closed

Occational null ref in CreateProxy #653

HaoK opened this issue Aug 7, 2018 · 2 comments

Comments

@HaoK
Copy link

HaoK commented Aug 7, 2018

We are seeing some rare null refs come from Moq: original issue: aspnet/Identity#1912

 var store = new Mock<IUserStore<TUser>>();
 var mgr = new Mock<UserManager<TUser>>(store.Object, null, null, null, null, null, null, null, null);

There's not too much going on here, but this sometimes fails rarely with:

System.NullReferenceException : Object reference not set to an instance of an object.
   at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
   at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
   at Castle.DynamicProxy.Generators.BaseProxyGenerator.ObtainProxyType(CacheKey cacheKey, Func`3 factory)
   at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors)
   at Moq.Proxy.CastleProxyFactory.CreateProxy(Type mockType, ICallInterceptor interceptor, Type[] interfaces, Object[] arguments)
   at Moq.Mock`1.<InitializeInstance>b__20_0()
   at Moq.Mock`1.OnGetObject()
   at Moq.Mock`1.get_Object()
   at Microsoft.AspNetCore.Identity.Test.MockHelpers.MockUserManager[TUser]() in /_/test/Shared/MockHelpers.cs:line 22
   at Microsoft.AspNetCore.Identity.Test.SignInManagerTest.SetupUserManager(PocoUser user)
   at Microsoft.AspNetCore.Identity.Test.SignInManagerTest.CanExternalSignIn(Boolean isPersistent, Boolean supportsLockout) in /_/test/Identity.Test/SignInManagerTest.cs:line 523

Any ideas what could be going on?

@stakx
Copy link
Contributor

stakx commented Aug 7, 2018

@HaoK, I cannot explain the failure without further digging, but I know these code paths quite well. At first glance, it appears that DynamicProxy is failing unexpectedly while trying to look up the requested proxy type in its type cache.

We have recently fixed some concurrency / threading issues in Castle DynamicProxy 4.3.1 related to ObtainProxyType (which is near the top of your stack trace)—see the DynamicProxy changelog entry and the relevant PR.

For this reason, I would suggest that you e.g. update Moq to version 4.9.0, which uses DynamicProxy 4.3.1. There's a slim chance that this might resolve your issue.

(Note also that we've had reports that upgrading Moq from 4.7 to 4.8 / 4.9 might cause some inital assembly binding problems because recent Moq versions reference some additional System.* NuGet packages related to ValueTask and ValueTuple. These binding redirect issues can usually be resolved quite easily. Just giving you a heads up in case you run into this.)

If an upgrade to Moq doesn't resolve this issue, we'll need to take a closer look.

Please let me know if you're OK with proceeding in this manner.

@stakx
Copy link
Contributor

stakx commented Aug 19, 2018

I am going to close this issue since there's nothing we can do at Moq's end for now. If this problem persists even after upgrading Moq to 4.9.0+ (or rather, after upgrading Castle.Core to 4.3.1+) then please post here again so we can reopen.

@stakx stakx closed this as completed Aug 19, 2018
@devlooped devlooped locked and limited conversation to collaborators Sep 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants