You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Class.dependency, which is monkey-patched in by the gem, creates an instance method for the dependency. This instance method does a check-and-set of an instance variable for the resolved dependency to ensure it queries the container for the instance only once.
Basically this:
classFoodependency:bar# etc.
creates this
classFoodefbarval=instance_variable_get(:@bar)ifval.nil?val=Sinject::Container.instance.get(:bar)instance_variable_set(:@bar,val)endvalend# etc.
This check-and-set memoization is not thread-safe.
Even though the dependency is not a singleton, foo.bar should always return the same instance. Therefore, objects.size should be 1.
Actual:
objects.size is greater than 1, the exact number being subject to the timing of the threads' execution.
Where objects.size is less than the number of threads spawned, this shows that eventually #bar does memoize the result and return the same instance on subsequent calls.
The text was updated successfully, but these errors were encountered:
Class.dependency
, which is monkey-patched in by the gem, creates an instance method for the dependency. This instance method does a check-and-set of an instance variable for the resolved dependency to ensure it queries the container for the instance only once.Basically this:
creates this
This check-and-set memoization is not thread-safe.
A contrived example:
Expected:
Even though the dependency is not a singleton,
foo.bar
should always return the same instance. Therefore,objects.size
should be 1.Actual:
objects.size
is greater than 1, the exact number being subject to the timing of the threads' execution.Where
objects.size
is less than the number of threads spawned, this shows that eventually#bar
does memoize the result and return the same instance on subsequent calls.The text was updated successfully, but these errors were encountered: