8

我有一个 Web 应用程序需要利用应用程序缓存来存储数据(由于在逐个请求的基础上获取该数据的开销很大)。请参阅https://stackoverflow.com/a/16961962/236860上的上一篇文章

这种方法似乎运作良好,但我在网站的错误中看到以下偶尔错误:

System.ApplicationException: Object synchronization method was called from an
unsynchronized block of code.
at System.Threading.Mutex.ReleaseMutex()
at InboxInsight.Web.Web_Controls.Twitter.TwitterFeed.GetTwitterData(HttpContext context)
at InboxInsight.Web.Web_Controls.Twitter.TwitterFeed.ProcessRequest(HttpContext context)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

作为参考,这里是代码块:

public string GetData(HttpContext context)
{
    var cache      = context.Cache;
    Mutex mutex    = null;
    string data    = (string)cache[CacheKey];

    // Start check to see if available on cache
    if (data == null)
    {
       try
       {
           // Lock base on resource key
           // (note that not all chars are valid for name)
           mutex = new Mutex(true, CacheKey);

           // Wait until it is safe to enter (someone else might already be
           // doing this), but also add 30 seconds max.
           mutex.WaitOne(30000);

           // Now let's see if some one else has added it...
           data = (string)cache[CacheKey];

           // They did, so send it...
           if (data != null)
           {
              return data;
           }


           // Still not there, so now is the time to look for it!
           data = GetSlowFeed(context);

           cache.Remove(CacheKey);
           cache.Add(CacheKey, data, null, GetExpiryDate(),
              TimeSpan.Zero, CacheItemPriority.Normal, null);
       }
       finally
       {
           // Release the Mutex.
           if (mutex != null)
           {
              mutex.ReleaseMutex();
           }
       }
    }

    return data;
}

根据我的研究,它表明这个问题是由一个进程线程试图释放它没有创建的互斥锁引起的,但我不明白这是怎么发生的。

谁能建议我如何重新构建代码以避免这个问题?

4

1 回答 1

15

您没有处理 mutex.WaitOne 返回 false 即超时的情况。如果 WaitOne 返回 false 您不拥有互斥锁,因此您不需要释放它。

bool iOwnTheMutex;

try {
    // set up mutex here...
    iOwnTheMutex = mutex.WaitOne(2000);
    if (iOwnTheMutex) {
       // do what you need to do
    }
}
finally {
    if (mutex != null && iOwnTheMutex) {
       mutex.ReleaseMutex();
    }
}    
于 2013-06-12T16:36:44.100 回答