Is ExplicitlySynchronized holding a lock?

michael.proepster's Avatar

michael.proepster

18 Sep, 2017 12:26 PM

Hi,

We are using ReaderWriterSynchronized aspect in this class.
I am trying to NOT hold a lock in this legacy code:
In Debug mode this works great, but in Release mode I get deadlocks. It looks like this code is holding a Reader lock.
Can you tell me what is going on?
How can I find out whether this code is actually holding the Reader lock or not?
Why does it work in Debug mode?

    [ExplicitlySynchronized]
    public void NotifyT1ContChanged()
    {
        if (T1contChanged != null)
        {
            ThreadPool.QueueUserWorkItem(NotifyT1ContChangedWorker);
        }
    }

    public event EventHandler T1contChanged;

    [ExplicitlySynchronized]
    private void NotifyT1ContChangedWorker(object state)
    {

            T1contChanged?.Invoke(this, new global::System.ComponentModel.PropertyChangedEventArgs("T1contChanged"));

    }
  1. Support Staff 1 Posted by PostSharp Techn... on 20 Sep, 2017 09:47 AM

    PostSharp Technologies's Avatar

    Hello,

    invoking event acquires a read lock for the duration of the event because the delegate may not be synchronized. I would suspect that the behavior difference would be due to reentrancy, which can be quite dependent on timing (especially on thread pool).

    However, I have managed to reproduce the deadlock and it is not detected by the deadlock detection (issue #15513) and also found out that writing from event handler to the same controller results in LockRecursionException (issue #15514), which is quite inconvenient. The latter was caused by a fix few releases back that was solving corrupted context stack (also invoking event handler from ExplicitlySynchronized private method).

    I'm aware that neither of these solves your problem, but I hope that I have managed to explain what is actually happening. Sadly there is currently no way to "unsynchronize" the event invoke.

    Best regards,
    Daniel

  2. Support Staff 2 Posted by PostSharp Techn... on 20 Sep, 2017 11:01 AM

    PostSharp Technologies's Avatar

    Hello,

    please ignore my comment about reentrancy causing the difference between debug and release. While it was the cause in my test, it's most likely that the difference you were experiencing was because of issue #15515 which I have described in the other ticket.

    Best,
    Daniel

  3. 3 Posted by michael.proepst... on 20 Sep, 2017 11:12 AM

    michael.proepster's Avatar

    Thanks for the explanation.
    I am looking forward to the fixes of #15513 and #15515

  4. Support Staff 4 Posted by PostSharp Techn... on 22 Nov, 2017 12:47 PM

    PostSharp Technologies's Avatar

    Hello,

    The bug #15515 has been fixed in the current versions of PostSharp 4.3.40 and 5.0.38. We expect that this also solves the reported issue with deadlocks.
    Should you need further help with this issue, don't hesitate to re-open this discussion.

    -alex

  5. PostSharp Technologies closed this discussion on 22 Nov, 2017 12:47 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac