Click here to Skip to main content
15,885,868 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Here is a part of a controller (I deal with ASP.net Web Api 2 ). When the controller receives a request from the client's side it starts and activates a Mutex instance. In CATCH block I realize the mutex instance (RealizeMutex method) but in the ttru block I have forgot to do this trick and it works, it performs like the have been releazed before? Just why? Thanks in advance.

namespace HMMM
{
    public class MYSTERYController : ApiController
    {
        public Mutex Mutec = new Mutex();
        public JObject MYSTERY([FromBody] string  SomeStuff)
        {
            Mutec.WaitOne();//attention
            try
            {
                //blablabla 
                //not released Mutex                  
                return Error.json("TheSameBlaBlaBla");
            }
            catch (Exception e)
            {
                Mutec.ReleaseMutex();//Attention
                return Error.json("SomeCoolError");
            }
        }

    }

} 
Posted
Comments
CHill60 23-Jul-15 12:28pm    
"If a thread terminates while owning a mutex, the mutex is said to be abandoned" - this can cause issues later.
Why not stick the release in a Finally block?
CPallini 23-Jul-15 13:34pm    
Virtual 5.

1 solution

This is just a bug. You need to guarantee that the mutex always released, so you should not release it under the "try" block, you should not release it under "catch" block; you should only release it in the "finally" block.

Are you saying that it all "works" properly without correctly releasing the mutex? There can be two explanations:
  1. Mutex does not do anything useful; in other words, you don't really have any shared resources protected by the mutex.
  2. Mutex (more exactly, not mutex, but some kind of mutual exclusion; see below) is really needed, but, just by a chance, your timing never got you to the situation where the shared resource is actually accessed non-exclusively. That would be the worse-case scenario. Your code could work "correctly" for months or years and got crashed somewhere during production.
    In other words, you could have created race condition, incorrect dependency on the time of execution, which is a deadly thing. Please see and understand thoroughly: http://en.wikipedia.org/wiki/Race_condition[^].

Whatever it is, but the code is wrong and dangerous. Do you need to use finally block instead? It depends.

First of all, it's likely that you don't need mutex. Is the mutex named? If not, you most certainly does not need it. If you don't have different processes working with the same shared resource, only then you may or may not need a mutex. In other cases, instead, you can use more lightweight lock statement: https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx[^].

Besides, it could be possible that you don't have mutual exclusion at all. Do you even have several threads? With ASP.NET, this is pretty unlikely. You need to understand when it is not needed and when not. Only remember: "the best synchronization is no synchronization". Please read this and understand thoroughly: https://en.wikipedia.org/wiki/Mutual_exclusion[^].

—SA
 
Share this answer
 
v3
Comments
CPallini 23-Jul-15 13:34pm    
5.
Sergey Alexandrovich Kryukov 23-Jul-15 14:45pm    
Thank you, Carlo.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900