Repository navigation
Conversation
| // Opportunistic cleanup | ||
| if (Interlocked.Increment(ref this._operationCount) >= CleanupThreshold) | ||
| { | ||
| Interlocked.Exchange(ref this._operationCount, 0); |
There was a problem hiding this comment.
There's a race condition here where multiple threads can bring us over the cleanup threshold (line 23) and all of them will trigger removal.
Not a huge deal, but I think it's free to fix:
if (Interlocked.Increment(ref this._operationCount) >= CleanupThreshold
// only run cleanup if we won the race in resetting the op count
&& Interlocked.Exchange(ref this._operationCount, 0) >= CleanupThreshold)
{
this.RemoveDeadEntries();
}
| } | ||
| } | ||
|
|
||
| internal static class ConcurrentDictionaryExtensions |
There was a problem hiding this comment.
Rather than an extension, since this is only used once let's just define a private static method in CWRD:
private static void Remove<T>(ICollection<T> collection, T value)
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved lock correctness issues let readers bypass waiting writers and allow pre-canceled read acquisitions to succeed, while cache and provider regression tests are excluded from CI.
4 open issues
What changed in this PR
Adds an in-process provider for DistributedLock’s existing abstractions, supporting local development, tests, and single-instance deployments. It also provides a local synchronization layer for the chaining use case described in #290.
Changes:
- Adds scoped mutexes, semaphores, and upgradeable reader-writer locks with shared weak-reference caches and abandoned-handle cleanup.
- Integrates the provider into the solution, code generation, and shared test infrastructure.
- Documents the provider’s APIs, scope isolation, and process-local limitations.
| File | Description |
|---|---|
| src/DistributedLockCodeGen/GenerateIDistributedLockImplementations.cs | Includes InProcess classes in interface generation. |
| src/DistributedLock.Tests/Tests/InProcess/InProcessPrimitiveCacheTest.cs | Tests cache concurrency and reuse after collection. |
| src/DistributedLock.Tests/Tests/InProcess/InProcessDistributedSynchronizationProviderTest.cs | Tests scopes and reader-writer behavior. |
| src/DistributedLock.Tests/Tests/CombinatorialTests.cs | Adds shared test fixtures for InProcess primitives. |
| src/DistributedLock.Tests/Infrastructure/TestingSynchronizationStrategy.cs | Adds a cross-process capability flag. |
| src/DistributedLock.Tests/Infrastructure/InProcess/TestingInProcessSynchronizationStrategy.cs | Isolates tests with unique provider scopes. |
| src/DistributedLock.Tests/Infrastructure/InProcess/TestingInProcessProviders.cs | Adapts the provider to shared test interfaces. |
| src/DistributedLock.Tests/DistributedLock.Tests.csproj | References the new provider project. |
| src/DistributedLock.Tests/AbstractTestCases/DistributedLockCoreTestCases.cs | Bypasses unsupported cross-process tests. |
| src/DistributedLock.sln | Registers the InProcess project. |
| src/DistributedLock.InProcess/SemaphoreSlimReleaser.cs | Releases semaphore tickets exactly once. |
| src/DistributedLock.InProcess/PublicAPI.Unshipped.txt | Records the new public API. |
| src/DistributedLock.InProcess/PublicAPI.Shipped.txt | Initializes the shipped API baseline. |
| src/DistributedLock.InProcess/packages.lock.json | Records provider dependencies. |
| src/DistributedLock.InProcess/InProcessReaderWriterLockType.cs | Defines reader-writer acquisition modes. |
| src/DistributedLock.InProcess/InProcessDistributedSynchronizationProvider.cs | Creates primitives shared by scope and name. |
| src/DistributedLock.InProcess/InProcessDistributedSemaphoreHandle.cs | Handles ticket release and abandonment cleanup. |
| src/DistributedLock.InProcess/InProcessDistributedSemaphore.IDistributedSemaphore.cs | Adds generated semaphore acquisition APIs. |
| src/DistributedLock.InProcess/InProcessDistributedSemaphore.cs | Implements semaphore-backed ticket acquisition. |
| src/DistributedLock.InProcess/InProcessDistributedReaderWriterLockHandle.cs | Implements reader-writer release and upgrades. |
| src/DistributedLock.InProcess/InProcessDistributedReaderWriterLock.IDistributedUpgradeableReaderWriterLock.cs | Adds generated reader-writer acquisition APIs. |
| src/DistributedLock.InProcess/InProcessDistributedReaderWriterLock.cs | Connects reader-writer APIs to shared state. |
| src/DistributedLock.InProcess/InProcessDistributedLockHandle.cs | Handles mutex release and abandonment cleanup. |
| src/DistributedLock.InProcess/InProcessDistributedLock.IDistributedLock.cs | Adds generated mutex acquisition APIs. |
| src/DistributedLock.InProcess/InProcessDistributedLock.cs | Implements semaphore-backed mutex acquisition. |
| src/DistributedLock.InProcess/DistributedLock.InProcess.csproj | Configures provider targets and packaging. |
| src/DistributedLock.InProcess/ConcurrentWeakReferenceDictionary.cs | Caches shared primitives without retaining unused values. |
| src/DistributedLock.InProcess/AsyncReaderWriterLockState.cs | Coordinates readers, writers, and upgrades. |
| src/DistributedLock.Core/Internal/TimeoutTracker.cs | Extracts reusable timeout tracking. |
| src/DistributedLock.Core/Internal/SemaphoreSlimExtensions.cs | Supports synchronous and asynchronous semaphore waits. |
| src/DistributedLock.Core/CompositeDistributedSynchronizationHandle.cs | Uses the extracted timeout tracker. |
| src/DistributedLock.Core/AssemblyAttributes.cs | Grants the provider access to Core internals. |
| README.md | Adds InProcess to the provider listing. |
| docs/DistributedLock.InProcess.md | Documents usage and synchronization semantics. |
Review effort: Balanced
| return await this._upgradeSemaphore.InternalWaitAsync(tracker.Remaining, cancellationToken).ConfigureAwait(false); | ||
|
|
||
| case InProcessReaderWriterLockType.Write: | ||
| if (!await this._upgradeSemaphore.InternalWaitAsync(tracker.Remaining, cancellationToken).ConfigureAwait(false)) |
| // gate. Every writer increments _writerCount under _readerCountLock before it tries to take _writeSemaphore, | ||
| // so while _writerCount == 0 the write semaphore is either free or held by the current readers. This avoids | ||
| // spurious zero-timeout failures caused by other readers briefly holding the entry gate. | ||
| lock (this._readerCountLock) |
|
|
||
| namespace Medallion.Threading.Tests.InProcess; | ||
|
|
||
| public class InProcessPrimitiveCacheTest |
| - **[DistributedLock.ZooKeeper](docs/DistributedLock.ZooKeeper.md)** [](https://www.nuget.org/packages/DistributedLock.ZooKeeper/) [](https://dndocs.com/d/distributedlock/api/Medallion.Threading.ZooKeeper.html) (uses Apache ZooKeeper) | ||
| - **[DistributedLock.FileSystem](docs/DistributedLock.FileSystem.md)** [](https://www.nuget.org/packages/DistributedLock.FileSystem/) [](https://dndocs.com/d/distributedlock/api/Medallion.Threading.FileSystem.html) (uses lock files) | ||
| - **[DistributedLock.WaitHandles](docs/DistributedLock.WaitHandles.md)** [](https://www.nuget.org/packages/DistributedLock.WaitHandles/) [](https://dndocs.com/d/distributedlock/api/Medallion.Threading.WaitHandles.html) (*Windows only*: uses operating system global `WaitHandle`s) | ||
| (*Windows only*: uses operating system global `WaitHandle`s) |


This adds a synchronization provider that is not at all distributed. Quite the contrary, it's in process.
The use cases: