SynchronizationLockException on Monitor.Exit when using await

You can’t await a task inside a lock scope (which is syntactic sugar for Monitor.Enter and Monitor.Exit). Using a Monitor directly will fool the compiler but not the framework.

async-await has no thread-affinity like a Monitor does. The code after the await will probably run in a different thread than the code before it. Which means that the thread that releases the Monitor isn’t necessarily the one that acquired it.

Either don’t use async-await in this case, or use a different synchronization construct like SemaphoreSlim or an AsyncLock you can build yourself. Here’s mine: https://stackoverflow.com/a/21011273/885318

Leave a Comment