Skip to content

[API thread-safety change] Make Dispose methods of sync primitives (SemaphoreSlim) thread safe #116735

Open
@petrroll

Description

@petrroll

While most of methods on sync primitives s.a. SemaphoreSlim are naturally thread safe not just with respect to themselves but also other methods on the type, Dispose / DisposeAsync is a notable exception.

Unlike most of the members of SemaphoreSlim, Dispose is not thread-safe and may not be used concurrently with other members of this instance.
https://learn.microsoft.com/en-us/dotnet/api/system.threading.semaphoreslim.dispose?view=net-9.0

While perfectly understandable from API design point of view, I'm convinced this is a pit of failure into which people fall and then end up getting very cryptic null refence exceptions.

Not only is it relatively unexpected, it's also hard to work around when one knows about the design. As if the dispose is e.g. triggered by cancellation token, one needs to have a synchronization primitive for a synchronization primitive and that makes the whole situation very messy, ouroboros, and again easy to mess up (developers are really bad at thread safety fixes it turns out).

I understand there are perf implications to solving this most likely. But I think it should at least be explored.

Furthermore, it not being an API shape change, it should not be as hard to get through.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions