-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Orphaned ReaderWriterLockSlim #12107
Comments
That is a general issue with any re-entrant lock on COM STA threads. You are not supposed have any locks in the code paths that are pumped by |
I'm happy for this to be closed since it happens due to a combination of several bad practices. However, I don't know if this explains why EnterWriteLock always works while EnterReadLock always (eventually) locks up. The stack never overflows, the call stack is identical every time, the worker thread is still happily running (and claiming then releasing its Write lock), and other than the time it takes to break there's nothing random happening. It really seems that there's a logic glitch somewhere that prevents the read lock attempt from ever getting notified that lock is now free. I've even had the worker thread exit several seconds after the GUI thread orphan has occurred, but it has no effect. |
|
EnterReadLock called on a ReaderWriterLockSlim inside a paint handler never return when this scenario occurs:
Worker thread claims the lock for Write access.
GUI thread [concurrently] attempts to claim the lock for Write access; it can't, so InternalWaitOne is called. The message queue is found to have a paint message so the OnPaint method gets called.
OnPaint tries to claim the lock for Read access. If the worker thread still has the Write lock then another InternalWaitOne is called but this never returns and the GUI locks, even though the worker thread releases its Write lock.
The problem does not occur if OnPaint is changed to claim Write access; this suggests some anomalous behaviour with EnterReadLock.
(I appreciate this is all very bad design, it is lifted from some legacy code I have to work on. But I don't expect that EnterReadLock should never return when the lock becomes unclaimed by any thread).
I've made a simple WinForms app to demonstrate this: https://github.com/nooogle/SlimLockTest
Breaking the app when it has locked shows this call stack on the GUI:
The text was updated successfully, but these errors were encountered: