.Text Threading Bug

If you are one of the 7 regular readers here you might have noticed some problems over the last few weeks. Every so often .Text would display an error page with the message: “Value cannot be null. Parameter name: value”. Once the error happened it would stick around until the application restarted. Unfortunately, the error was appearing everyday. After asking around on some of the boards to no avail I did some sleuthing.

In .Text 0.95 the Stats class has the following method:

public static bool AddQuedStats(EntryView ev)
{
    //Check for the limit
    if(queuedStatsList.Count >= queuedAllowCount)
    {
        //aquire the lock
        lock(queuedStatsList.SyncRoot)
        {
            //make sure the pool queue was not cleared during a wait for the lock
            if(queuedStatsList.Count >= queuedAllowCount)
            {
                EntryView[] eva = new EntryView[queuedStatsList.Count];
                queuedStatsList.CopyTo(eva,0);
 
                ClearTrackEntryQueue(new EntryViewCollection(eva));
                queuedStatsList.Clear();            
            }
        }
    }
    queuedStatsList.Add(ev);
    return true;
}

The first highlighted method is in the call stack when the exception is thrown, but like any good threading bug the problem actually begins somewhere else: queuedStatsList.Add. The Add method is not thread safe and the collection eventually corrupts with a null reference appearing in a slot where an EntryView object reference should be. The EntryViewCollection ctor barfs when it tries to copy the null reference. Because the application can never clear the queue the exception keeps occurring until reset. I only checked 0.96 briefly, but it looks like the problem still exists. If you are running into this problem, one fix is to change the code and move the Add inside of the lock scope (and skip the double check locking):

public static bool AddQuedStats(EntryView ev)
{
    //aquire the lock
    lock(queuedStatsList.SyncRoot)
    {
        if(queuedStatsList.Count >= queuedAllowCount)
        {
            EntryView[] eva = new EntryView[queuedStatsList.Count];
            queuedStatsList.CopyTo(eva,0);
            ClearTrackEntryQueue(new EntryViewCollection(eva));
            queuedStatsList.Clear();         
            
        }
        queuedStatsList.Add(ev);
    }
    return true;
}

If you are having this problem and don’t want to recompile the application, set queueStats=”false” in the Tracking section of web.config.

It seems like I am the only one who was having this problem, which is odd because I certainly don’t have the same number of concurrent users as, say, blogs.msdn.com, but that’s multithreading for you.

I do want to say thanks to ScottW for all his .Text work, hopefully this will add just a little bit of improvement to some great software.

posted on Wednesday, May 26, 2004 9:00 PM by scott

Comments

Friday, May 28, 2004 9:02 AM by

# re: High-performance multithreading is very hard

Wednesday, August 25, 2004 11:36 AM by

# Ran into my first blog hosting bug... that was fast...

Wednesday, August 25, 2004 1:43 PM by

# Couple of issues fixed on blogs.vbcity.com

Sunday, September 05, 2004 9:54 PM by

# .Text のバグ 「値を Null にすることはできません。パラメータ名 : value」

.Text ??? ??? Null ?????????????????? : value?
Tuesday, October 19, 2004 11:30 AM by

# Technical difficulties

Wednesday, January 12, 2005 9:45 PM by

# Strange .Text Bug Resolved

Friday, January 14, 2005 1:08 PM by Sean Chase

# re: .Text Threading Bug

Thanks! I just ran into this bug myself and your work-around worked like a champ.
Wednesday, March 09, 2005 8:54 PM by

# Wahoo!! My blog is fixed for now...

Saturday, March 12, 2005 4:53 PM by

# Wahoo!! My blog is fixed for now...

Sunday, April 24, 2005 8:26 PM by

# Wahoo!! My blog is fixed for now...

I Googled the problem, and found this article http://odetocode.com/Blogs/scott/archive/2004/05/26/259.aspx...
Tuesday, November 29, 2005 7:04 AM by The Angry Coder

# Fix for article problem on STGBlog

Friday, December 16, 2005 1:06 AM by Code/Tea/Etc.

# Ran into my first blog hosting bug... that was fast...

Sunday, August 06, 2006 6:14 AM by Benjamin Day Consulting, Inc.: The Blog

# Mysterious errors from dotText (.Text)

Wednesday, June 13, 2007 12:30 PM by /egilh

#