[Mono-dev] Bug in TimeoutDispatcher makes it into a spinwait when there is nothing in the queue

Dan Smith dan at algenta.com
Fri Aug 27 14:22:40 EDT 2010

Hi all,

First time mono-devel poster. I was working with bitsharp and discovered 
a bug in the TimeoutDispatcher [1] that coverts it into a cpu intensive 
spinwait. It is also present in another variation in the UPNP library[2]

Currently (line 154):
TimeSpan interval = hasItem ? item.Trigger - DateTime.UtcNow : 
TimeSpan.FromMilliseconds (-1);
if (interval < TimeSpan.Zero) {
   interval = TimeSpan.Zero;
if (!wait.WaitOne (interval, false) && hasItem) {

The AutoResetEvent interval is never set to -1 (infinite wait) because 
of the < zero check. This makes the TimeoutDispatcher a spinwait if 
there are no items in the queue.

The Zero check should only be performed if there is an item in the 
dispatcher that has set the interval time. Something along the lines of:

TimeSpan interval = TimeSpan.FromMilliseconds(-1);
if (hasItem)
   interval = item.Trigger - DateTime.UtcNow;
   if (interval < TimeSpan.Zero)
     interval = TimeSpan.Zero;

Please let me know if you need any more information.


Dan Smith
+1 608-213-2867
Algenta Technologies, LLC

More information about the Mono-devel-list mailing list