Skip to content

If an attempt to acquire a System.Threading.Lock ends up throwing, we should leave the lock unlocked. #97240

@VSadov

Description

@VSadov

Noticed while reviewing #97227

The acquisition path tries to acquire the lock under try

I assume the purpose of that is to catch exceptions that may come from the event source, which is external code.
Since we may see exception after the lock is already in acquired state, the catch should release the lock, but instead it unregisters a waiter.

catch // run this code before exception filters in callers
{
State.UnregisterWaiter(this);
throw;
}

I think it would be unexpected for the caller that a failed attempt to acquire could leave the lock in a locked state. Worst case the calling thread will terminate while leaving behind an acquired lock, which is UB.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions