﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Panteleimon commented on Challenge: Your own ThreadLocal</title><description>Can any one give an example or explain where CloseableThreadLocal will be helpful?
  
Example in MSDN 
[msdn.microsoft.com/ru-ru/library/dd642243.aspx](http://msdn.microsoft.com/ru-ru/library/dd642243.aspx) looks like academic, but not real.
  
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment24</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment24</guid><pubDate>Wed, 22 Dec 2010 07:16:55 GMT</pubDate></item><item><title>Bernhard Hofmann commented on Challenge: Your own ThreadLocal</title><description>Why use [ThreadStatic] when you're not using a new thread for each CloseableThreadLocal?
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment23</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment23</guid><pubDate>Mon, 20 Dec 2010 08:05:11 GMT</pubDate></item><item><title>tobi commented on Challenge: Your own ThreadLocal</title><description>Thomas, you need to add locking around the global HashSet. Not sure how performant this is compared to just using LocalDataStoreSlot because the locks will give up many performance gains.
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment22</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment22</guid><pubDate>Sat, 18 Dec 2010 19:03:25 GMT</pubDate></item><item><title>Thomas Eyde commented on Challenge: Your own ThreadLocal</title><description>I finally got it. And no need for weak references.
  
  
Key points, as previously mentioned in this thread:
  
1. We need a thread global reference to track each thread local slot so the finalizer can reach it.
  
  
2. We need to remove the strong reference introduced by the dictionary key. I went for a local Guid.
  
  
3. The finalizer must call Close
  
  
4. Handle new potential memory leaks introduced by code changes.
  
  
Code and tests here: 
[http://pastebin.com/x26qnpLn](http://pastebin.com/x26qnpLn)  
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment21</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment21</guid><pubDate>Sat, 18 Dec 2010 15:13:19 GMT</pubDate></item><item><title>Justin Chase commented on Challenge: Your own ThreadLocal</title><description>Don't use ThreadStatic. Create an instance for whatever scope you want this functionality and dispose of it properly when you leave that scope.
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment20</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment20</guid><pubDate>Fri, 17 Dec 2010 22:13:14 GMT</pubDate></item><item><title>Ryan Heath commented on Challenge: Your own ThreadLocal</title><description>I agree that a list of dictionaries is saver, but it is questionable that one is using an instance of this class in multiple threads at once.
  
Smells like a bug...
  
  
// Ryan
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment19</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment19</guid><pubDate>Fri, 17 Dec 2010 18:16:35 GMT</pubDate></item><item><title>Patrick Huizinga commented on Challenge: Your own ThreadLocal</title><description>hmm, made mistake in criticizing jonnii; ctl does get GC'ed.
  
  
Anyway, after shooting down jonnii, it's only fair he gets a shot at my attempt :-)
  
  
class ClosableThreadLocal
  
{
  
[ThreadStatic] static Dictionary slots;
  
readonly List usedSlots = new List();
  
  
public Dictionary Slots
  
{
  
get
  
{
  
if (slots != null) return slots;
  
  
slots = new Dictionary();
  
lock (usedSlots) usedSlots.Add(slots);
  
return slots;
  
}
  
}
  
  
public object Key { get { return usedSlots; } }
  
  
public void Set(object value)
  
{
  
Slots[Key] = value;
  
}
  
  
public void Close()
  
{
  
if (slots == null) return;
  
  
slots.Remove(Key);
  
lock (usedSlots) usedSlots.Remove(slot);
  
}
  
  
void Dispose()
  
{
  
foreach(Dictionary slot in usedSlots) slot.Remove(Key);
  
}
  
}
  
  
As you can see it's mostly the same code as yours (jonnii), except that I keep a list with all used dictionaries, so I can actually enumerate them all and clean them all out.
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment18</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment18</guid><pubDate>Fri, 17 Dec 2010 16:24:37 GMT</pubDate></item><item><title>Patrick Huizinga commented on Challenge: Your own ThreadLocal</title><description>jonnii,
  
This will break your code:
  
  
var ctl = new CloseableThreadLocal()
  
new Thread(() =&gt; ctl.Set(new BigObject())).Start();
  
new Thread(() =&gt; ctl.Set(new BigObject())).Start();
  
  
It will not GC one of the BigObjects. Also ctl will never be GC'ed.
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment17</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment17</guid><pubDate>Fri, 17 Dec 2010 16:08:48 GMT</pubDate></item><item><title>jonnii commented on Challenge: Your own ThreadLocal</title><description>This is my answer.
  
  
  
    public class CloseableThreadLocal
  
    {
  
        [ThreadStatic]
  
        private static Dictionary
&lt;object,&gt;
 slots;
  
  
        private readonly object key = new object();
  
        private Dictionary
&lt;object,&gt;
 localref;
  
  
        public static Dictionary
&lt;object,&gt;
 Slots
  
        {
  
            get
  
            {
  
                return slots ?? (slots = new Dictionary
&lt;object,&gt;
());
  
            }
  
        }
  
  
        public /*protected internal*/ virtual Object InitialValue()
  
        {
  
            return null;
  
        }
  
  
        public Dictionary
&lt;object,&gt;
 LocalSlots
  
        {
  
            get { return localref = Slots; }
  
        }
  
  
        public virtual Object Get()
  
        {
  
            object val;
  
  
            if (LocalSlots.TryGetValue(key, out val))
  
            {
  
                return val;
  
            }
  
            val = InitialValue();
  
            Set(val);
  
            return val;
  
        }
  
  
        public virtual void Set(object val)
  
        {
  
            LocalSlots[key] = val;
  
        }
  
  
        public virtual void Close()
  
        {
  
            if (localref != null)
  
            {
  
                // intentionally using the field here, to avoid creating the instance
  
                localref.Remove(key);
  
            }
  
        }
  
  
        ~CloseableThreadLocal()
  
        {
  
            Close();
  
        }
  
    }
  
  
Use an object as the key, so that you don't end up with a reference to yourself and also keep a local reference to the dictionary so that you get a chance to remove yourself.
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment16</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment16</guid><pubDate>Fri, 17 Dec 2010 15:46:40 GMT</pubDate></item><item><title>Daniel Grunwald commented on Challenge: Your own ThreadLocal</title><description>Patrick Huizinga: But there's the following strong reference chain:
  
ThreadStatic -&gt; slots -&gt; Values -&gt; Leak -&gt; CloseableThreadLocal
  
  
Therefore, the finalizer of CloseableThreadLocal doesn't get invoked, and the entry is never removed from the slots.
  
Yes, WeakReference on its own can handle cycles; it's the static dictionary that's introducing the trouble here. And you can't solve the problem by introducing more WeakReferences. What you need is a CONDITIONAL weak reference: it's a strong reference as long as the key is alive; but weak otherwise.
  
  
In GC literature, this kind of reference is called "Ephemerons" (
[http://en.wikipedia.org/wiki/Ephemeron](http://en.wikipedia.org/wiki/Ephemeron)).
  
Ephemeron support is new in the .NET 4.0 garbage collector, and ConditionalWeakTable is the public API for it.
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment15</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment15</guid><pubDate>Fri, 17 Dec 2010 13:57:24 GMT</pubDate></item><item><title>Patrick Huizinga commented on Challenge: Your own ThreadLocal</title><description>Daniel, I'm pretty sure WeakReference can deal with cyclic references, since the GC can handle them too.
  
The GC will determine the only way to reach your Leak instance is through a WeakReference, which it should disregard, and therefore clean up your instance.
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment14</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment14</guid><pubDate>Fri, 17 Dec 2010 13:15:17 GMT</pubDate></item><item><title>Daniel Grunwald commented on Challenge: Your own ThreadLocal</title><description>This comment box seems to be eating generics...
  
  
I meant using a Dictionary[of WeakReference, object] and finalizer.
  
  
Rob and Ryan posted in the meantime, but I think both their approaches are incorrect:
  
Rob is missing the finalizer; and Ryan is clearing only one dictionary - but I would expect that dropping the reference to the CloseableThreadLocal gets rid of the values on ALL threads (I would expect the same of the Close() method, so this issue is already present in Ayende's code).
  
But I like how Ryan avoided the WeakReference by simply using another object as key.
  
  
So instead of the "Dictionary localSlots" (race condition!), I would use a "List[of Dictionary] allDicts" to store the dictionaries of all threads on which this CloseableThreadLocal was used. Except that opens up the possibility of referencing the dictionaries of threads that exited, so you might have to use something like a list of weak references to the dictionaries.
  
  
But I don't see how anything except ConditionalWeakTable can stop this simple class from leaking:
  
class Leak {
  
    CloseableThreadLocal local;
  
    public Leak() {
  
       local = new CloseableThreadLocal();
  
       local.Set(this);
  
    }
  
}
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment13</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment13</guid><pubDate>Fri, 17 Dec 2010 12:29:33 GMT</pubDate></item><item><title>SHSE commented on Challenge: Your own ThreadLocal</title><description>[http://pastebin.com/yjLYtnW0](http://pastebin.com/yjLYtnW0)</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment12</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment12</guid><pubDate>Fri, 17 Dec 2010 12:15:59 GMT</pubDate></item><item><title>Daniel Grunwald commented on Challenge: Your own ThreadLocal</title><description>Here are some additional problems to consider:
  
- The Dictionary is keeping the key object alive, so a finalizer will never be called (unless the thread exits).
  
- The value object may have a reference to the CloseableThreadLocal
  
 (key object). Such a cyclic reference shouldn't prevent us from garbage collecting the object.
  
  
The first problem alone could be worked around by using a Dictionary
&lt;weakreference,&gt;
 and adding a finalizer. But I don't see any way to work around the second problem using .NET 3.5. There doesn't seem to be any way to distinguish between outstanding references from GC roots and outstanding references from our cycles.
  
  
In .NET 4.0, it's easy: just replace the Dictionary
&lt;object,object&gt;
 with a ConditionalWeakTable
&lt;object,object&gt;
.
  
  
Then again, the .NET 4.0 ThreadLocal class also doesn't seem to solve the problem with cyclic references; so I guess a Dictionary
&lt;weakreference,&gt;
 is good enough for most usecases.
  
But I like how the .NET 4.0 implementation manages to directly use the fast [ThreadStatic] support without having to go through slow hash tables.
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment11</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment11</guid><pubDate>Fri, 17 Dec 2010 12:14:28 GMT</pubDate></item><item><title>Ryan Heath commented on Challenge: Your own ThreadLocal</title><description>There are two problems to overcome:
  
- the class is inserting itself into the dictionary so it can never be collected
  
- when above problem is fixed, the finalizer is running on a different thread, so we must keep a ref to the slots created in main thread.
  
  
public class CloseableThreadLocal
  
{
  
  [ThreadStatic] private static Dictionary
&lt;object,&gt;
 slots;
  
  
  object key = new Object();
  
  Dictionary
&lt;object,&gt;
 localSlots = null;
  
  
  public static Dictionary
&lt;object,&gt;
 Slots
  
  {
  
    get { return slots ?? (slots = new Dictionary
&lt;object,&gt;
()); }
  
  }
  
  
  Dictionary
&lt;object,&gt;
 LocalSlots
  
  {
  
    get { return slots ?? ( localSlots = Slots); }
  
  }
  
  
  public /*protected internal*/ virtual Object InitialValue()
  
  {
  
    return null;
  
  }
  
  
  public virtual Object Get()
  
  {
  
    object val;
  
  
    if (LocalSlots.TryGetValue(key, out val))
  
    {
  
      return val;
  
    }
  
    val = InitialValue();
  
    Set(val);
  
    return val;
  
  }
  
  
  public virtual void Set(object val)
  
  {
  
    LocalSlots[key] = val;
  
  }
  
  
  public virtual void Close()
  
  {
  
    if (localSlots != null)// intentionally using the field here, to avoid creating the instance
  
      localSlots.Remove(key);
  
  }
  
  
  ~CloseableThreadLocal()
  
  {
  
    Close();
  
  }
  
}
  
  
// Ryan
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment10</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment10</guid><pubDate>Fri, 17 Dec 2010 12:14:20 GMT</pubDate></item><item><title>Rob Ashton commented on Challenge: Your own ThreadLocal</title><description>The best solution I can come up with WeakReference (I'm at work so I'm having to do this on idle ;-)
  
  
  
        public class CloseableThreadLocal
  
        {
  
            [ThreadStatic]
  
            private static Dictionary
&lt;object,&gt;
 slots;
  
  
            private WeakReference self;
  
  
            public CloseableThreadLocal() { self = new WeakReference(this); }
  
  
            public static Dictionary
&lt;object,&gt;
 Slots
  
            {
  
                get { return slots ?? (slots = new Dictionary
&lt;object,&gt;
()); }
  
            }
  
  
            public /*protected internal*/ virtual Object InitialValue()
  
            {
  
                return null;
  
            }
  
  
            public virtual Object Get()
  
            {
  
                object val;
  
  
                if (Slots.TryGetValue(self, out val))
  
                {
  
                    return val;
  
                }
  
                val = InitialValue();
  
                Set(val);
  
                return val;
  
            }
  
  
            public virtual void Set(object val)
  
            {
  
                Slots[self] = val;
  
            }
  
  
            public virtual void Close()
  
            {
  
                if (slots != null)// intentionally using the field here, to avoid creating the instance
  
                    slots.Remove(self);
  
            }
  
        }
  
  
That gives us the ability to add another method to clear anything in the dictionary in the current thread when an object is created on that thread (It's a bit sweep and cleany). (We can check the dictionary for any weak references that haven't got a valid reference anymore)
  
  
Seems a bit overkill though given we're going for as few changes as possible, I suspect there is something I'm missing here.
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment9</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment9</guid><pubDate>Fri, 17 Dec 2010 12:12:34 GMT</pubDate></item><item><title>tobi commented on Challenge: Your own ThreadLocal</title><description>And I just peeked under the covers on .net 4. the implementation is really creative and quite funny ;-) I am very keen to find out about how to do this with "minimal changes".
  
  
Here is a potential way: Let all access to the dictionary pass through a threadlocal spinlock. When creating the CloseableThreadLocal in the ctor, copy the dict and the lock to instance variables so that they are available on the finalizer. On the finalizer call close. I understand locks are very cheap if they are nearly always uncontended and thread-local (which would be the case here).
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment8</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment8</guid><pubDate>Fri, 17 Dec 2010 11:15:52 GMT</pubDate></item><item><title>Steve Py commented on Challenge: Your own ThreadLocal</title><description>Rob's right, Cannot rely on a finalizer. Frankly I'd choose the big stick approach. Implement IDisposable, and a finalizer that throws if Dispose wasn't called.
  
  
private bool _isDisposed = false;
  
void IDisposable.Dispose()
  
{
  
	Dispose(true);
  
}
  
  
protected virtual void Dispose(bool disposing)
  
{
  
	if (!_isDisposed)
  
	{
  
		if (disposing)
  
			Close();
  
	       _isDisposed = true;
  
	}
  
}
  
  
~CloseableThreadLocal()
  
{
  
	if (!_isDisposed)
  
	{
  
#if DEBUG
  
		Debugger.Break();
  
#endif
  
		throw new InvalidOperationException("Disposable object was not disposed. (CloseableThreadLocal)");
  
	}
  
}
  
  
It is a memory leak because on a long-lived thread repeated use of new ThreadLocal instances will continue to pile up items in the static dictionary. If Close isn't called those items are never removed.
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment7</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment7</guid><pubDate>Fri, 17 Dec 2010 11:13:37 GMT</pubDate></item><item><title>Thomas Eyde commented on Challenge: Your own ThreadLocal</title><description>Using WeakReference is the first thing that pops into my mind
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment6</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment6</guid><pubDate>Fri, 17 Dec 2010 11:11:20 GMT</pubDate></item><item><title>tobi commented on Challenge: Your own ThreadLocal</title><description>Ok, finalization happens on a different thread... thats nasty! Looks like i am still not done with learning.
  
  
You could first convert the dictionary into a hashset and store the value in the instance. Then you change the hashset to a weakhashset (not built-in). then you introduce a threadstatic int counter which you use to purge the dead items on every 1000th operation. what a hack...
  
  
depending on the perf requirements i would just use a localdatastoreslot which is built-in on 2.0 upwards.
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment5</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment5</guid><pubDate>Fri, 17 Dec 2010 11:10:44 GMT</pubDate></item><item><title>Rafal commented on Challenge: Your own ThreadLocal</title><description>But how does the memory leak occur? [ThreadLocal] objects should be GCd after the thread terminates, so the only possibility is that the thread doesn't terminate and is reused for other purposes, like in a thread pool.
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment4</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment4</guid><pubDate>Fri, 17 Dec 2010 10:55:38 GMT</pubDate></item><item><title>Rob Ashton commented on Challenge: Your own ThreadLocal</title><description>Thinking about this now, 
  
  
IDisposable doesn't solve anything, it just gives the class a pattern that people recognise so they're more likely to remember to call Close.
  
  
Using a finalizer is a great idea, except you've got a ThreadStatic there, and you don't know what thread the finalizer will be called in.
  
  
(So calling Close from a finalizer will not work)
  
  
Okay, so I've given my opinion on why I think those won't work, I'm still thinking about the best solution =)
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment3</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment3</guid><pubDate>Fri, 17 Dec 2010 10:48:14 GMT</pubDate></item><item><title>Steve Py commented on Challenge: Your own ThreadLocal</title><description>Well, my guess based on the behaviour you describe is to make the class disposable. On dispose, call Close. Anyone implementing the class should be checking for IDisposable and be "using" appropriately.
  
  
Other than that it may be a candidate scenario for calling Close() within a finalizer, though that test scenario won't pass with the finalizer implementation likely due to compiler optimization, but I believe it would prevent the memory leak.
  
  
I'd opt for best practice though.
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment2</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment2</guid><pubDate>Fri, 17 Dec 2010 10:42:03 GMT</pubDate></item><item><title>tobi commented on Challenge: Your own ThreadLocal</title><description>Implement IDisposable for the common case and Finalize() for the uncommon/bug case. The finalizer could throw if a debugger was attached to assist in finding leaks.
</description><link>http://ayende.com/4726/challenge-your-own-threadlocal#comment1</link><guid>http://ayende.com/4726/challenge-your-own-threadlocal#comment1</guid><pubDate>Fri, 17 Dec 2010 10:40:13 GMT</pubDate></item></channel></rss>