Compze.Threading.Interprocess
0.20.0-alpha
Prefix Reserved
dotnet add package Compze.Threading.Interprocess --version 0.20.0-alpha
NuGet\Install-Package Compze.Threading.Interprocess -Version 0.20.0-alpha
<PackageReference Include="Compze.Threading.Interprocess" Version="0.20.0-alpha" />
<PackageVersion Include="Compze.Threading.Interprocess" Version="0.20.0-alpha" />
<PackageReference Include="Compze.Threading.Interprocess" />
paket add Compze.Threading.Interprocess --version 0.20.0-alpha
#r "nuget: Compze.Threading.Interprocess, 0.20.0-alpha"
#:package Compze.Threading.Interprocess@0.20.0-alpha
#addin nuget:?package=Compze.Threading.Interprocess&version=0.20.0-alpha&prerelease
#tool nuget:?package=Compze.Threading.Interprocess&version=0.20.0-alpha&prerelease
Compze.Threading.Interprocess
Cross-process synchronization for Compze.Threading. It brings the same pit-of-success locking model — impossible to forget to lock, automatic deadlock diagnosis with dual stack traces — across process boundaries, backed by the operating system's named mutexes.
Extracted from Compze.Threading 0.7.0-alpha so the cross-process primitives can version independently of the
in-process ones. It depends on Compze.Threading for the shared ICriticalSection / IAwaitableCriticalSection
abstractions that both the in-process and cross-process implementations satisfy — so an IMutex is an
ICriticalSection and an IAwaitableMutex is an IAwaitableCriticalSection, interchangeable with their
in-process counterparts wherever the abstraction is what matters.
What it adds
IMutex — a cross-process lock that diagnoses its own deadlocks
A named system Mutex wrapped so it behaves like the rest of Compze.Threading: Global (visible across all
login sessions) and Local (one session) variants, cancellation-token support, abandoned-mutex recovery, and
the same timeout-and-dual-stack-trace deadlock diagnostics as the in-process IMonitor.
IAwaitableMutex — Monitor.Wait semantics across processes
The full condition-wait model — TakeUpdateLockWhen(condition), TakeReadLockWhen(condition), and the
TryTake* variants — working across processes. It releases every nesting level of the lock while waiting and
reacquires on signal, mirroring Monitor.Wait exactly. As far as we know, no other .NET library exposes the
full Monitor/condition-wait model over a cross-process mutex.
using var mutex = IAwaitableMutex.Global(new FileInfo(Path.Combine(directory.FullName, "MyResource.signal")));
using(mutex.TakeUpdateLockWhen(() => ResourceIsReady()))
{
// Exclusive across every process on the machine; the wait released the lock
// for other processes and reacquired it once the condition held.
}
The async condition waits — TryAwaitAsync, ReadWhenAsync, UpdateWhenAsync, TryUpdateWhenAsync — work across processes
too, and hold no thread while they wait: between polls of the signal (see how a wait polls, below) nothing waits but a timer.
IProcessLock — a lock between processes that any thread can release
A lock on an IMutex can only be released by the thread that took it, so async code can't hold it across an await,
nor can an xUnit fixture take it as it is created and release it as it is disposed. An IProcessLock belongs to its
holder instead: disposing the IProcessLockHold that taking it returns releases it, from whichever thread. The
operating system releases it when the holder's process dies, so a crashed or killed process never leaves it held.
var sandboxTurn = IProcessLock.New(new FileInfo(Path.Combine(directory.FullName, "sandbox-turn.lock")));
using(await sandboxTurn.TakeAsync("running the sandbox specifications", WaitTimeout.Minutes(20)))
{
// Every other process taking the lock over this file waits until the hold is disposed.
}
The wait is a WaitTimeout, not a LockTimeout: a holder may rightly hold it for minutes, so a long wait is no sign
of a deadlock. A Take that gives up says what the lock is held for, by which process, and since when;
TryTake(purpose, WaitTimeout.Zero) answers at once. The lock is its file, which its holder keeps open, and a wait
tries it every 50 milliseconds: it isn't fair, nor reentrant.
On Windows an ordinary reader can keep the lock file open while the holder changes. Ownership comes from an OS
byte-range lock. Read LastHoldersDescription() for diagnostics. It reads {file}.holder and understands the
description an earlier Windows version wrote in the lock file. Both versions exclude each other. An earlier
version can report an outdated or missing description for a current holder.
IProcessShared<T> / IAwaitableProcessShared<T>
Pair a shared object with a cross-process mutex to coordinate access to an external resource — a file, a port,
a database — across processes. Each process has its own object: for an object the processes really share, see
IInterprocessObject<T> below.
IInterprocessObject<T> — an object shared between processes, in a single line of code
An IAwaitableProcessShared<T> whose object every process shares: kept in a file, so a change one
process makes is visible to all the others at once, and the object survives the processes and reboots.
- Atomic updates: every read and update holds the object's cross-process mutex. The file has two slots: an update writes the new value into the slot that doesn't hold the committed one, then commits it in one 8-byte store. A killed writer leaves the committed value available to readers.
- No file opened per read or update: each instance keeps the file open and mapped into memory until it is disposed,
so a virus scanner that scans each file opened, as Windows Defender does, scans it once per instance, not once per
update. Another program that opens the file meanwhile must share it for reading and writing (
FileShare.ReadWrite). - Room: the file reserves room for two values of
maxBytes, plus a 16-byte header. An instance given a largermaxBytesthan the file has room for grows the file, and the instances that have it open map it again. - Directory access: grant the participating processes permission to create and replace files in the chosen directory,
where the object creates its file, and set data access restrictions on that directory, so they also cover the files
ReplaceIfUnreadablekeeps. - Waits for conditions: the same condition waits as
IAwaitableMutex, with the object loaded for each evaluation of the condition and saved after an update. - Unreadable state:
InterprocessObjectDeserializationExceptionreports the file, inFilePath, and the cause. The bytes remain available to a compatible reader. Code that knows starting over is safe callsReplaceIfUnreadable(value), which, under the mutex, writes the value only while the content can't be read, and keeps the unreadable bytes in a new file beside the object's, returning its path: no other process finds the object missing meanwhile. Processes that fail to read it at once can each call it: the first writes, and the others find its value, write nothing and get null. By hand: stop every process using the file, delete it, and restart them. - Pluggable serialization: JSON with
IInterprocessObjectSerializer.Json<T>(), or a serializer of your own, such as one in MemoryPack's binary form.
using var visits = IInterprocessObject.NewGlobal(new FileInfo(Path.Combine(directory.FullName, "visits.json")),
IInterprocessObjectSerializer.Json<Visits>(), () => new Visits(), maxBytes: 4 * 1024);
visits.Update(it => it.Count++);
It keeps more files beside its own, named after it: {file}.signal, the signal of its mutex, and {file}.pending, the
file it stages before creating its file, or replacing a file in a format it can't read. Deleting the folder deletes them
all.
It came here from Compze.InterprocessObject, which is retired: its types are in the namespace
Compze.Threading.Interprocess.ResourceAccess now.
The file is the identity
IAwaitableMutex, IAwaitableProcessShared<T>, IInterprocessObject<T> and IProcessLock are each opened over a
file, which is all that identifies them: every process on the machine that opens one over the same file shares it,
whichever user's and in whichever login session, and different files share nothing. Two spellings of one path, such as
a relative one and an absolute one, or on Windows one in other capitals, are one file. The mutex of an awaitable mutex
is named by a hash of its file's full path; another path to the same file, through a link, a junction or a short 8.3
name, is another mutex. A process lock is the file itself, so any path to it is the same lock.
There are no variants scoped to a login session: whoever can reach the file shares it, so its lock must reach them all. Who can share a primitive is who can open its file, but any process on the machine can take the system mutex of an awaitable mutex, and so stall those who share it.
Only IMutex and IProcessShared<T>, which keep no files, are identified by a name, Global or Local.
Earlier versions named the mutex by a name the caller gave, so a process of an earlier version and one of this version never exclude each other: processes that share an awaitable mutex or an awaitable process-shared value move to this version together. An interprocess object's file begins with its format, which no earlier version can read, and it refuses a file an earlier version wrote, so the two versions can't share an object's file, whatever file a consumer gives. Each format change does the same: format version 2, of versions 0.17 to 0.19, and format version 3, of this version, refuse each other. A process lock's file is the lock in every version, so they exclude each other.
IInterprocessObject.FileFormatVersion is the version of that format, which changes only when the format does. An
object shared with applications built against other versions of Compze.Threading.Interprocess, such as a machine-wide
state, gets a file named by it, such as state_f{FileFormatVersion}.mmf: each format then has a file of its own. It is
read when the application runs, so it names the format of the version the application runs on.
How a wait polls: signal latency against power
Awaiting a cross-process condition polls a memory-mapped change counter, because .NET has no portable cross-process kernel signal. Each poll wakes the CPU, and frequent wakeups keep it out of its deep low-power idle states — a real battery cost on laptops. So a wait backs off: it polls eagerly at first, when a signal is most likely to come soon, then ever less often, until its polls are 50 ms apart.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Compze.Contracts (>= 0.7.3)
- Compze.Threading (= 0.20.0-alpha)
NuGet packages (5)
Showing the top 5 NuGet packages that depend on Compze.Threading.Interprocess:
| Package | Downloads |
|---|---|
|
Compze.Tessaging
Messaging infrastructure for the Compze framework including command, query, and event handling. |
|
|
Compze.Sql.Sqlite
Discontinued: this is its last version. Compze no longer supports SQLite: it runs one writer at a time, so it could never be a fully supported persistence layer. Use Compze.Sql.MicrosoftSql for SQL Server, Compze.Sql.PostgreSql for PostgreSQL or Compze.Sql.MySql for MySQL. SQLite support for the Compze framework: declares the SQLite domain database that Compze endpoints join. Also carries the SQLite plumbing the Compze.*.Sqlite feature backends share. |
|
|
Compze.Hosting
Endpoint hosting for the Compze framework — endpoints, endpoint hosts, and the builder that capabilities such as the Tessaging and Typermedia pipelines plug into. |
|
|
Compze.Teventive.MemoryImageStateStore
A memory image of taggregates: every taggregate held in memory for the container's life, rebuilt at its start from a snapshot of each, which JSON files, or a database's tables, keep. A taggregate store that keeps taggregates as their state, not their history. |
|
|
Compze.Internals.Testing
For Compze internal use only. Do not take a dependency on this package |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.20.0-alpha | 0 | 12/5/2026 |
| 0.19.0-alpha | 0 | 12/5/2026 |
| 0.18.0-alpha | 0 | 12/5/2026 |
| 0.17.1-alpha | 0 | 12/5/2026 |
| 0.17.0-alpha | 7 | 12/5/2026 |
| 0.16.4-alpha | 76 | 12/5/2026 |
| 0.16.3-alpha | 203 | 12/5/2026 |
| 0.16.2-alpha | 184 | 12/4/2026 |
| 0.16.1-alpha | 78 | 12/4/2026 |
| 0.16.0-alpha | 314 | 12/3/2026 |
| 0.15.0-alpha | 90 | 12/3/2026 |
| 0.14.0-alpha | 89 | 12/2/2026 |
| 0.13.0-alpha | 277 | 12/2/2026 |
| 0.12.0-alpha | 383 | 11/29/2026 |
| 0.11.3-alpha | 153 | 11/28/2026 |
| 0.11.2-alpha | 143 | 11/28/2026 |
| 0.11.1-alpha | 243 | 11/28/2026 |
| 0.11.0-alpha | 122 | 11/27/2026 |
| 0.10.0-alpha | 107 | 11/26/2026 |
| 0.9.0-alpha | 131 | 11/26/2026 |