🔧 Fix a System.Threading.Lock That Silently Falls Back to Monitor Semantics Because It Was Passed Around as an Ordinary Object Instead of the Lock Type
.NET 9’s System.Threading.Lock is a dedicated synchronization type, and the C# compiler specifically recognizes it inside a lock statement: when the compile-time static type of a lock statement’s expression is exactly Lock, the compiler emits a call to Lock’s own EnterScope method, which returns a ref struct and gives you the type’s real performance and diagnostic benefits over the classic Monitor-based pattern. Lock also implements an explicit, backward-compatible path through object’s own monitor-style Enter/Exit so that existing code written against object-based locking doesn’t immediately break, and that compatibility path is exactly what silently kicks in the moment a Lock instance gets stored in, passed as, or returned through a variable whose static type is object rather than Lock — the lock statement still compiles and still provides mutual exclusion, it just quietly uses the older Monitor semantics instead of the new type’s actual locking mechanism, with nothing at compile time flagging that the optimization you were expecting silently didn’t apply.
🔎 The Problem
public class OrderProcessor
{
private readonly Lock _lock = new();
// Mistake: exposing the lock through an 'object' typed member
// (common when retrofitting into an API that predates Lock,
// or when storing it in a loosely-typed cache/dictionary)
public object SyncRoot => _lock;
public void ProcessBatch()
{
lock (_lock) // static type here is Lock - uses the new,
{ // fast EnterScope() path correctly
// ...
}
}
}
// Elsewhere in the codebase:
var processor = new OrderProcessor();
object syncRoot = processor.SyncRoot; // static type is now 'object'
lock (syncRoot) // compiles fine - but the compiler sees the
{ // expression's static type as 'object', not Lock,
// ... // so it emits the OLD Monitor.Enter/Exit(object)
} // pattern instead of Lock's own EnterScope() path -
// mutual exclusion still works correctly, but none
// of Lock's improved diagnostics or performance
// characteristics actually apply to this call site,
// and nothing warns that the "upgrade" silently
// didn't take effect here.
✅ Fix: Keep Lock’s Static Type Visible Everywhere You Actually Lock On It
- Expose a Lock field or property as Lock, not as object, at every point where code might later write a lock statement against it — the compiler’s special-case behavior depends entirely on the expression’s compile-time static type being exactly Lock at the lock statement itself.
- Avoid storing a Lock instance in a loosely-typed container (an object array, a string-keyed dictionary of objects, a generic cache) if any consumer of that container needs to lock on the retrieved value afterward, since retrieving it back out as object permanently loses the static typing the fast path depends on.
- If a Lock absolutely must cross an API boundary that’s typed as object for compatibility reasons, cast it back to Lock explicitly at the point you lock on it ((Lock)syncRoot) rather than locking on the object-typed reference directly, which restores the compiler’s special-case handling for that specific call site.
🚨 Why This Is Dangerous
- Because both code paths produce code that compiles cleanly and correctly provides mutual exclusion, there’s no error, warning, or runtime exception anywhere to indicate the newer, intended locking behavior silently didn’t apply — the only symptom is not getting a performance or diagnostic benefit you reasonably expected to have after upgrading to Lock.
System.Threading.Lock’s better behavior is a compile-time decision based on the exact static type the compiler sees at the lock statement, and passing it around as a plain object is a perfectly legal way to quietly opt back out of that decision without ever being told you did.
