[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) {
.....


Issue:
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.


Solution:
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.
Cheers!
Dan

[1] 
http://github.com/mono/bitsharp/blob/master/src/MonoTorrent/MonoTorrent.Common/TimeoutDispatcher.cs
[2] 
http://github.com/mono/mono-upnp/blob/master/src/Mono.Ssdp/Mono.Ssdp/Mono.Ssdp.Internal/TimeoutDispatcher.cs

-- 
Dan Smith
+1 608-213-2867
Algenta Technologies, LLC
http://www.algenta.com
http://www.colectica.com


More information about the Mono-devel-list mailing list