Skip to content

Possible occurrence of write lock leakage during high concurrency. #3

Open
@ghostclearsky

Description

@ghostclearsky

There is a defect in Wintellect.Threading.ResourceLocks.ResourceLock.Leave where write lock leakage can easily occur in high concurrency scenarios.

The Enter method first acquires the lock and then increments the counter, while the Leave method first releases the lock and then decrements the counter.

This introduces a possibility where, assuming thousands of threads are executing read locks simultaneously and releasing them, and at that moment, one thread acquires a write lock and immediately releases it. In this scenario, it is possible that the counter for the read locks has not yet completed the subtraction operation. When the Leave method is called on this write lock, it mistakenly treats it as a read lock and releases it accordingly. This triggers an exception stating "Read lock was not held when attempting to release," resulting in write lock leakage.

To address this issue, the Leave method should be modified to place the lock release operation (OnLeave(exclusive)) at the bottom. This ensures the atomicity of the lock.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions