title: How to manage a mutex in an asynchronous method
body: <p>I have ported my old HttpHandler (.ashx) TwitterFeed code to a WebAPI application.  The core of the code uses the excellent Linq2Twitter package (<a href="https://linqtotwitter.codeplex.com/" rel="noreferrer">https://linqtotwitter.codeplex.com/</a>).  Part of the port involved upgrading this component from version 2 to version 3, which now provides a number of asynchronous method calls - which are new to me.  Here is the basic controller:</p>
  
  <pre><code>public async Task&lt;IEnumerable&lt;Status&gt;&gt; 
  GetTweets(int count, bool includeRetweets, bool excludeReplies)
  {
     var auth = new SingleUserAuthorizer
     {
        CredentialStore = new SingleUserInMemoryCredentialStore
        {
           ConsumerKey       = ConfigurationManager.AppSettings["twitterConsumerKey"],
           ConsumerSecret    = ConfigurationManager.AppSettings["twitterConsumerKeySecret"],
           AccessToken       = ConfigurationManager.AppSettings["twitterAccessToken"],
           AccessTokenSecret = ConfigurationManager.AppSettings["twitterAccessTokenSecret"]
        }
     };
  
     var ctx = new TwitterContext(auth);
  
     var tweets =
        await
        (from tweet in ctx.Status
           where (
              (tweet.Type == StatusType.Home)
              &amp;&amp; (tweet.ExcludeReplies == excludeReplies)
              &amp;&amp; (tweet.IncludeMyRetweet == includeRetweets)
              &amp;&amp; (tweet.Count == count)
           )
        select tweet)
        .ToListAsync();
  
     return tweets;
  }
  </code></pre>
  
  <p>This works fine, but previously, I had cached the results to avoid 'over calling' the Twitter API.  It is here that I have run into a problem (more to do with my lack of understanding of the asynchronous protocol than anything else I suspect).</p>
  
  <p>In overview, what I want to do is to first check the cache, if data doesn't exists, then rehydrate the cache and return the data to the caller (web page).  Here is my attempt at the code</p>
  
  <pre><code>public class TwitterController : ApiController {
  
     private const string CacheKey = "TwitterFeed";
  
     public async Task&lt;IEnumerable&lt;Status&gt;&gt;
     GetTweets(int count, bool includeRetweets, bool excludeReplies)
     {
        var context = System.Web.HttpContext.Current;
        var tweets = await GetTweetData(context, count, includeRetweets, excludeReplies);
        return tweets;
     }
  
     private async Task&lt;IEnumerable&lt;Status&gt;&gt;
     GetTweetData(HttpContext context, int count, bool includeRetweets, bool excludeReplies)
     {
        var cache = context.Cache;
        Mutex mutex = null;
        bool iOwnMutex = false;
        IEnumerable&lt;Status&gt; data = (IEnumerable&lt;Status&gt;)cache[CacheKey];
  
        // Start check to see if available on cache
        if (data == null)
        {
           try
           {
              // Lock base on resource key
              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.
              iOwnMutex = mutex.WaitOne(30000);
  
              // Now let's see if some one else has added it...
              data = (IEnumerable&lt;Status&gt;)cache[CacheKey];
  
              // They did, so send it...
              if (data != null)
              {
                 return data;
              }
  
              if (iOwnMutex)
              {
                 // Still not there, so now is the time to look for it!
                 data = await CallTwitterApi(count, includeRetweets, excludeReplies);
  
                 cache.Remove(CacheKey);
                 cache.Add(CacheKey, data, null, GetTwitterExpiryDate(),
                    TimeSpan.Zero, CacheItemPriority.Normal, null);
              }
           }
           finally
           {
              // Release the Mutex.
              if ((mutex != null) &amp;&amp; (iOwnMutex))
              {
                 // The following line throws the error:
                 // Object synchronization method was called from an
                 // unsynchronized block of code.
                 mutex.ReleaseMutex();
              }
           }
        }
  
        return data;
     }
  
     private DateTime GetTwitterExpiryDate()
     {
        string szExpiry = ConfigurationManager.AppSettings["twitterCacheExpiry"];
        int expiry = Int32.Parse(szExpiry);
        return DateTime.Now.AddMinutes(expiry);
     }
  
     private async Task&lt;IEnumerable&lt;Status&gt;&gt;
     CallTwitterApi(int count, bool includeRetweets, bool excludeReplies)
     {
        var auth = new SingleUserAuthorizer
        {
           CredentialStore = new SingleUserInMemoryCredentialStore
           {
              ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"],
              ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerKeySecret"],
              AccessToken = ConfigurationManager.AppSettings["twitterAccessToken"],
              AccessTokenSecret = ConfigurationManager.AppSettings["twitterAccessTokenSecret"]
           }
        };
  
        var ctx = new TwitterContext(auth);
  
        var tweets =
           await
           (from tweet in ctx.Status
            where (
               (tweet.Type == StatusType.Home)
               &amp;&amp; (tweet.ExcludeReplies == excludeReplies)
               &amp;&amp; (tweet.IncludeMyRetweet == includeRetweets)
               &amp;&amp; (tweet.Count == count)
               &amp;&amp; (tweet.RetweetCount &lt; 1)
            )
            select tweet)
           .ToListAsync();
  
        return tweets;
     }
  
  }
  </code></pre>
  
  <p>The problem occurs in the finally code block where the Mutex is released (though I have concerns about the overall pattern and approach of the GetTweetData() method):</p>
  
  <pre><code>if ((mutex != null) &amp;&amp; (iOwnMutex))
  {
      // The following line throws the error:
      // Object synchronization method was called from an
      // unsynchronized block of code.
      mutex.ReleaseMutex();
  }
  </code></pre>
  
  <p>If I comment out the line, the code works correctly, but (I assume) I should release the Mutex having created it.  From what I have found out, this problem is related to the thread changing between creating and releasing the mutex.</p>
  
  <p>Because of my lack of general knowledge on asynchronous coding, I am not sure a) if the pattern I'm using is viable and b) if it is, how I address the problem.</p>
  
  <p>Any advice would be much appreciated.</p>