PerWebRequestLifestyleManager not thread safe?

15 views
Skip to first unread message

Nicolás Sabena

unread,
Jun 27, 2011, 1:52:50 PM6/27/11
to Castle Project Development List
Hi. I think I found a little bug in the PerWebRequestLifestyleManager
that causes it not to release certain components (or releasing them
earlier than expected) when many requests end at the same time for
components of the same type (same instance of LifestyleManager).

This happens in the 2.5.3 release. The code in the main branch changed
substancially and this probably got fixed, but this bug may cause
leaks for people using this last release.

The problem is with the way the manager handles the internal Evict()
method:

internal void Evict(object instance)
{
using (new EvictionScope(this))
{
// that's not really thread safe, should we care
about it?
Kernel.ReleaseComponent(instance);
}
}

The commentary itself (from the actual code) signals the problem.
EvictionScope sets a class internal member, "evicting", to true while
in the scope, so that the Release() method does the actual release:

public override bool Release(object instance)
{
if (evicting == false) return false;

return base.Release(instance);
}

But since one PerWebRequestLifestyleManager is responsable for all
components of the same type, and "evicting" applies to the lifestyle
manager as a whole, it can happen that while one thread sets or clears
the EvictionScope, another thread expects another value, and an
instance misses the Release() (or get released early).

The solution seems to be locking the evict operation:

private object o = new object();
internal void Evict(object instance)
{
// lock so nobody else changes evicting while releasing.
lock (o)
{
using (new EvictionScope(this))
{
Kernel.ReleaseComponent(instance);
}
}
}


What do you think about this?

Thanks a lot,
Nicolás.

whut

unread,
Sep 21, 2011, 1:45:53 AM9/21/11
to castle-pro...@googlegroups.com
This looks quite scary. Is anybody from Castle looking at this?

Krzysztof Koźmic

unread,
Sep 21, 2011, 2:09:55 AM9/21/11
to castle-pro...@googlegroups.com
This is fixed in v3.

If someone wants to provide a patch for v2.5 I'm happy to release it as
v2.5.4

Krzysztof

On 21/09/2011 3:45 PM, whut wrote:
> This looks quite scary. Is anybody from Castle looking at this?

> --
> You received this message because you are subscribed to the Google
> Groups "Castle Project Development List" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/castle-project-devel/-/BGMigmnyeCUJ.
> To post to this group, send email to
> castle-pro...@googlegroups.com.
> To unsubscribe from this group, send email to
> castle-project-d...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/castle-project-devel?hl=en.

Reply all
Reply to author
Forward
0 new messages