none
If an exception is thrown inside a lock() {}, will the lock be released or not? RRS feed

  • Question

  • If an exception is thrown within a lock block is the lock released or not?

    Example of a class wherein exceptions will easily be thrown while a lock is in effect:

    namespace Utils
    {
        public class ThreadSafeDictionary<TKey, TValue>
        {
            private object LockObj = new object();
            private Dictionary<TKey, TValue> Dic = new Dictionary<TKey, TValue>();
    
            public TValue this[TKey key] 
            { 
                get
                {
                    TValue val = default(TValue);
    
                    lock(LockObj)
                    {
                        // What if an exception happens?
                        // Will LockObj be released?
                        val = Dic[key];
                    }
    
                    return val;
                }
                set
                {
                    lock (LockObj)
                    {
                        // What if an exception happens?
                        // Will LockObj be released?
                        Dic[key] = value;
                    }
                }
            }
        }
    }


    Richard Lewis Haggard


    Friday, January 31, 2020 11:02 PM

Answers

  • Seems to me the documentation indicates that the lock is released -- lock statement (C# reference)

    -------------

    The lock statement is of the form

    C#
    lock (x)
    {
        // Your code...
    }
    

    where x is an expression of a reference type. It's precisely equivalent to

    C#
    object __lockObj = x;
    bool __lockWasTaken = false;
    try
    {
        System.Threading.Monitor.Enter(__lockObj, ref __lockWasTaken);
        // Your code...
    }
    finally
    {
        if (__lockWasTaken) System.Threading.Monitor.Exit(__lockObj);
    }
    

    Since the code uses a try...finally block, the lock is released even if an exception is thrown within the body of a lock statement.

    • Proposed as answer by MasaSam Monday, February 3, 2020 5:53 AM
    • Marked as answer by Richard.Haggard Monday, February 3, 2020 3:06 PM
    Saturday, February 1, 2020 2:17 AM

All replies

  • Use a try-catch, otherwise an exception will be thrown

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, January 31, 2020 11:49 PM
    Moderator
  • Seems to me the documentation indicates that the lock is released -- lock statement (C# reference)

    -------------

    The lock statement is of the form

    C#
    lock (x)
    {
        // Your code...
    }
    

    where x is an expression of a reference type. It's precisely equivalent to

    C#
    object __lockObj = x;
    bool __lockWasTaken = false;
    try
    {
        System.Threading.Monitor.Enter(__lockObj, ref __lockWasTaken);
        // Your code...
    }
    finally
    {
        if (__lockWasTaken) System.Threading.Monitor.Exit(__lockObj);
    }
    

    Since the code uses a try...finally block, the lock is released even if an exception is thrown within the body of a lock statement.

    • Proposed as answer by MasaSam Monday, February 3, 2020 5:53 AM
    • Marked as answer by Richard.Haggard Monday, February 3, 2020 3:06 PM
    Saturday, February 1, 2020 2:17 AM
  • Hi Richard,

    Thank you for posting here.

    The answer is yes, it will be released.

    Looking at this link, I think what they said is clear.

    Does a locked object stay locked if an exception occurs inside it?

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 3, 2020 8:55 AM