Multithreading is hard.

| | Comments (2) | TrackBacks (2)

Lately at work I've been dealing with a problematic socket server. The currently deployed version has something of a memory leak (to the tune of 140+MB/day), probably due to complications of incorrectly multithreading System.Net.Socket instances (note: they're not thread-safe).

Unfortunately, when I redid the socket server to lock all the sockets and other non-thread-safe resources, I ran into a deadlock. In chasing it down, I used Phil Haack's modification of Ian Griffith's TimedLock class. That enabled me to find where the deadlocks were, and eliminate them. This class is really a very clever tool, with one small problem: it was throwing exceptions on the production server. The test server ran fine for days at a time, loaded down as heavily as I could manage, but the production server locked inside of two hours every time. The first error in the log was always an ArgumentException thrown by the stack trace hashtable, saying that the object being inserted as the key was already in the hashtable.

After several days of debugging, and a few e-mails exchanged with Phil, he said the following to me:

If the object wasn't removed from the hashtable via the dispose method before the second lock is acquired, that could cause the error.

I started to write back, saying "But isn't the whole point of the locking that there is no way any other thread could acquire that lock until Dispose is called, thus calling Monitor.Exit and removing the object from the hashtable?", and then I was, as they say, enlightened. The sequence of events in the TimedLock runs like this:

TimedLock tl = TimedLock.Lock(o);
  Monitor.TryEnter(o);
  StackTraces.Add(o);
...
tl.Dispose();
  Monitor.Exit(o);
  StackTraces.Remove(o);

On a single-CPU machine (such as our test server), this code runs fine, I would guess, 99.99999% of the time. On a dual-cpu machine (such as the production server in question), however, it runs fine only 99% of the time. That 100th time, here's what happens...(assuming o is the same object in both threads)

Thread A                              Thread B
TimedLock tl = TimedLock.Lock(o);
  Monitor.TryEnter(o);
  StackTraces.Add(o);                 TimedLock tl = TimedLock.Lock(o);
...                                     Monitor.TryEnter(o); // blocked
...                                   ...waiting
...                                   ...waiting
tl.Dispose();                         ...waiting
  Monitor.Exit(o);                    ...waiting
                                        StackTraces.Add(o); //******
  StackTraces.Remove(o);

The starred line is where the exception gets thrown. Textbook race condition -- if Thread B doesn't hit that Add() call between Thread A's calls to Monitor.Exit and StackTraces.Remove, then everything looks fine. But every once in a while (such as when processing a send and a receive simultaneously on a socket), it'll hit that tiny little target and blow the whole thing up.

What's worse is that as written, once that target has been hit, that object can't be successfully TimedLocked (even though the original lock has been released) until the TimedLock that hit the exception has been finalized. This is true even if you wrap the TimedLock in a using statement (because the exception will leave using() with a null reference, which it can't Dispose).

The fix? Simple -- swap the order of the Monitor.Exit() and StackTraces.Remove() calls. That ensures that the object will be removed from the hash table before any other thread can try to re-add it.

This all looks very cut and dry now that I've laid it out, but before anyone goes accusing Phil of not knowing his stuff, reread the subject of this post. Multithreading is hard. .Net (and other modern languages) do a good job of hiding some of the complexity; for most WinForms apps, for instance, threading is very easy as long as you remember to use InvokeRequired and Invoke. For something more complex, for instance a server app with multiple long-running threads that must access common resources, you need some help, and writing that help can be very difficult. It took me about 3 full days to find this bug, and all I have to say at the end is that if I weren't using a good helper class like TimedLock, it would have taken me much, much longer.

One other lesson I've (re)learned... always always always test multithreaded code on a multiprocessor machine, because it's so much easier to hit race conditions and other problems on that platform.

2 TrackBacks

Listed below are links to blogs that reference this entry: Multithreading is hard..

TrackBack URL for this entry: http://www.randomtree.org/cgi-bin/mt-trackbacks.cgi/106

» TimedLock with Stack Traces Strikes Back from you've been HAACKED

TITLE: TimedLock with Stack Traces Strikes Back URL: http://haacked.com/archive/0001/01/01/1341.aspx IP: 66.102.135.210 BLOG NAME: you've been HAACKED DATE: 10/13/2004 07:09:38 PM Read More

TITLE: TimedLock class: helping find and avoid deadlocks in your multithreaded code URL: http://weblogs.asp.net/rosherove/archive/2005/04/08/397655.aspx IP: 66.129.67.202 BLOG NAME: ISerializable DATE: 04/08/2005 05:19:35 AM Read More

2 Comments

Haacked said:

Glad it worked out. However, I just posted a new version of the TimedLock that takes an entirely different approach in tracking down deadlocks.

I'd give you the direct link, but my blog is running very slowly right now. ;)

In any case, I've tackled the multi-threaded Socket Server problem as well and hope to post that soon. I hope you read my discussion and Ian's in-depth treatise on asynchronous socket operations. :)

Eric said:

No worries... I have your feed subscribed. ;) This whole socket server thing has been a several month process for me, and an incredibly valuable learning experience. :)

Leave a comment

About this Entry

This page contains a single entry by Eric published on October 13, 2004 2:13 PM.

Innovative--or at least creative--recruiting was the previous entry in this blog.

CopySourceAsHtml is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 4.01