Skip to content

NullReferenceException in ExternallyControlledLifetimeManager #114

@appel1

Description

@appel1
[TestMethod]
public void NullReferenceExceptionWhenGettingAndSettingsValuesFromMultipleThreads()
{
var e = new ExternallyControlledLifetimeManager();

Parallel.For(
  0,
  100000,
  i =>
  {
    if (i % 2 == 0)
      e.SetValue(new Foo());
    else
    {
      var foo = e.GetValue();
    }
  });

public class Foo
{
  public int[] Value { get; } = new int[1024 * 1024];
}
}

If we resolve values registered with ExternallyControlledLifetimeManager from multiple threads we quite frequently get a NullReferenceException in ExternallyControlledLifetimeManager.GetValue. There are no locks as far as I can tell anywhere so it boils down to what happens with the code above.

System.NullReferenceException: Object reference not set to an instance of an object.
   at Unity.Lifetime.ExternallyControlledLifetimeManager.GetValue(ILifetimeContainer container)
   at Unity.Strategies.LifetimeStrategy.PreBuildUp(BuilderContext& context)
   at Unity.UnityContainer.<>c.<.ctor>b__73_1(BuilderContext& context)
   at Unity.UnityContainer.Unity.IUnityContainer.Resolve(Type type, String name, ResolverOverride[] overrides)
   at Unity.UnityContainerExtensions.Resolve[T](IUnityContainer container, String name, ResolverOverride[] overrides)

This happens with Unity.Container 5.11.1.0 and Unity.Abstractions 4.2.0.0.
It did not happen with Unity.Container 5.8.13.0 and Unity.Abstractions 3.3.1.0.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions