Skip to content

Feature: In process synchronization provider - #291

Open
gliljas wants to merge 2 commits into
madelson:masterfrom
gliljas:InProcess
Open

gliljas wants to merge 2 commits into
madelson:masterfrom
gliljas:InProcess

Conversation

@gliljas

@gliljas gliljas commented Oct 15, 2026 •

Copy link
Copy Markdown

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

The use cases:

  1. Local development
  2. Tests
  3. Use the same code if the specific deployment is not distributed
  4. Use together with Feature: Chained synchronization #290.

// Opportunistic cleanup
if (Interlocked.Increment(ref this._operationCount) >= CleanupThreshold)
{
Interlocked.Exchange(ref this._operationCount, 0);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I like that! Will fix.

}
}

internal static class ConcurrentDictionaryExtensions

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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
Comment thread README.md
- **[DistributedLock.ZooKeeper](docs/DistributedLock.ZooKeeper.md)** [![NuGet Status](http://img.shields.io/nuget/v/DistributedLock.ZooKeeper.svg?style=flat)](https://www.nuget.org/packages/DistributedLock.ZooKeeper/) [![Static Badge](https://img.shields.io/badge/API%20Docs-DNDocs-190088?logo=readme&logoColor=white)](https://dndocs.com/d/distributedlock/api/Medallion.Threading.ZooKeeper.html) (uses Apache ZooKeeper)
- **[DistributedLock.FileSystem](docs/DistributedLock.FileSystem.md)** [![NuGet Status](http://img.shields.io/nuget/v/DistributedLock.FileSystem.svg?style=flat)](https://www.nuget.org/packages/DistributedLock.FileSystem/) [![Static Badge](https://img.shields.io/badge/API%20Docs-DNDocs-190088?logo=readme&logoColor=white)](https://dndocs.com/d/distributedlock/api/Medallion.Threading.FileSystem.html) (uses lock files)
- **[DistributedLock.WaitHandles](docs/DistributedLock.WaitHandles.md)** [![NuGet Status](http://img.shields.io/nuget/v/DistributedLock.WaitHandles.svg?style=flat)](https://www.nuget.org/packages/DistributedLock.WaitHandles/) [![Static Badge](https://img.shields.io/badge/API%20Docs-DNDocs-190088?logo=readme&logoColor=white)](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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants