I stucked into a problem with threads syncronization and critical sections on Windows 10.
Application will crash in this case:
- Application has two threads.
- Thread 1 calls EnterCriticalSection with object m_CS
- Thread 2 then attempts to enter the same critical section
- Thread 1 terminates Thread 2 using TerminateThread
- Thread 1 calls LeaveCriticalSection
In previous Windows versions which I was able to test (7, 8, 8.1) this works properly. Thread 2 terminates, and Thread 1 leaves the critical section without exception.
On Windows 10, when Thread 1 leaves the critical section, application crashes with Access Violation. It only happens when another thread was terminated while waiting on EnterCriticalThread.
Looking at the stack trace it looks this (latest frame at the top):
RtlpWakeByAddress
RtlpUnWaitCriticalSection
RtlLeaveCriticalSection
I spent so much time on debugging this issue. In my case m_CS is totally fine when LeaveCriticalSection was called. I debugged and spent some time to analyze disassembled code of ntdll.dll functions. Seems like object corrupts somewhere during execution of RtlpUnWaitCriticalSection and then passed to RtlpWakeByAddress when crash occurs. Basicly ntdll.dll was able to modify CRITICAL_SECTION object's properties such as lock count in RtlLeaveCriticalSection.
From the web I didn't find any answer on this or statement what changed in Windows 10. Only thread on reddit and ~1800 crash reports for Mozilla Firefox with same call stack in the last month. I contacted with author of post on reddit and he was not able to fix this thus far.
So anybody dealed with this issue and may be have a fix for it or advices? As a solution right now I only see to rethink usage of WinAPI TerminateThread and try to avoid it as much as possible. Another way probably to do a code refactoring and think on application's architecture.
Any response appreciated. Thanks in advance
TerminateThread. You may find a workaround for Windows 10 build X, but that isn't even guaranteed to work with Windows 10 build X+1. You did not find documentation on what changed in Windows 10, because implementation details change all the time. Therefore, don't rely on them. - MSaltersTerminateThreadis a horrible idea. The only viable fix is to stop callingTerminateThread. - IInspectable