Crash caused by orphaned locks #44071
Labels
area-System.Threading
bug
in-pr
There is an active PR which will close this issue when it is merged
tenet-reliability
Reliability/stability related issue (stress, load problems, etc.)
Milestone
I just run into an issue on .NET Framework, but it impacts .NET Core as well. In both cases, it causes the app to crash with an access violation.
Repro code:
First, a thread locks an object. A thin-lock is used. The thread dies without releasing the lock.
Later, the thin-lock is promoted to a syncblock (either because of a call to
GetHashCode
, or because another thread tries to acquire the lock). When it does, the runtime tries to map the thin-lock id to the address of the thread: https://github.com/dotnet/runtime/blob/master/src/coreclr/src/vm/syncblk.cpp#L2214If the cleaned up thread was the last in the map, it causes the
IdToThreadWithValidation
method to return NULL (that's theif ((size_t)result <= m_idToThreadCapacity)
check) :runtime/src/coreclr/src/vm/threads.h
Lines 5160 to 5174 in 4d09ba6
If it does, then
GetSyncBlock
sets the thread address in the syncblock to -1:runtime/src/coreclr/src/vm/syncblk.cpp
Lines 2216 to 2220 in 4d09ba6
Later, another thread tries to acquire the lock.
ObjectNative::IsLockHeld
is called, which in turn callsGetThreadOwningMonitorLock
(https://github.com/dotnet/runtime/blob/master/src/coreclr/src/vm/syncblk.cpp#L1804). The method fetches the thread address from the syncblock, checks if it's null, but omits to check if it's equal to -1. This causes the access violation when trying to dereference it to fetch the thread id:runtime/src/coreclr/src/vm/syncblk.cpp
Lines 1840 to 1852 in 4d09ba6
The text was updated successfully, but these errors were encountered: