Replace persistent mode-admission sidecars with process-wide OS-native database locks
Context
Follow-up to #3023 / #3019.
#3023 currently introduces a persistent <database>-shared-mode file for Direct/Shared admission. That adds another coordination artifact with its own identity, initialization, crash, permission, orphan-recovery and compatibility rules.
For basic mode admission, prefer solid OS locking rather than another persistent LiteDB protocol file.
Goal
Introduce one process-wide admission lease per physical database, ref-counted across every LiteDatabase / LiteEngine in that process.
The first local user acquires the OS-level database admission lock. Additional compatible local users only increase the process-local reference count. The last user releases the OS handle/lock.
No persistent -shared-mode identity file should be required.
Desired semantics
Direct
- Direct writable access owns an exclusive database admission lease.
- A second process trying Direct access fails closed.
- Shared participants cannot attach while Direct owns the lease.
- Multiple compatible Direct objects inside the same process may share the one process-wide lease.
- The lease remains held until the last compatible local database/engine releases it.
Shared
- Shared participants acquire a shared admission lease.
- Multiple Shared processes may coexist.
- A Direct/exclusive participant cannot enter while any Shared lease exists.
- Shared's existing mutex/MMF machinery may continue to coordinate actual operations; this admission lease is only the outer mode-compatibility fence.
Platform strategy
The implementation may intentionally differ by OS.
Candidate primitives to evaluate:
- Windows: native file/range locking (for example
LockFileEx) on the database file/handle, with shared vs exclusive admission semantics. - Linux/macOS: advisory file locking such as
flock/appropriate native equivalent, again with shared vs exclusive semantics.
Do not force one abstraction onto every OS if native semantics differ. Define the required LiteDB invariants first and implement the strongest reliable primitive per platform.
Avoid thread-affine ownership for the process-lifetime lease: acquisition and final release may happen on different managed threads.
Process-wide registry
Introduce a process-local registry keyed by a canonical physical database identity, conceptually:
- canonical path / physical identity;
- OS admission handle;
- mode: shared or exclusive;
- reference count;
- compatibility checks for subsequent local opens.
This should also prevent a process from accidentally opening the same database through two incompatible LiteDB modes just because both opens originate locally.
Path aliases/symlinks/hard links need explicit handling or clearly documented limits; where practical prefer physical file identity over string-path identity.
Rebuild / file replacement
A lock tied to the current database file needs an explicit handoff protocol when rebuild/upgrade replaces that file.
Do not create an unlock window during publication.
The online rebuild/cutover design is tracked separately; this issue should define how the admission lease follows or safely transfers across replacement of the live file.
Interaction with existing Shared coordination
The existing -shared-live, -shared-state, -shared-disabled files/MMF serve the mapped Shared fast path and are a separate concern.
This issue is specifically about replacing #3023's new mode-admission sidecar. Do not require redesigning the Shared MMF protocol in the same change.
Required invariants
- Direct + Shared conflict fails before writable storage access.
- Direct + Direct cross-process conflict fails.
- Shared + Shared remains allowed.
- Process crash automatically releases admission.
- No stale persistent admission artifact can block a database forever.
- Last local release actually releases the OS lease.
- Ref-count races cannot release a lease while another local engine/database still depends on it.
- Open failure must unwind its acquired local reference.
- Finalizer/abnormal disposal paths must not strand process-local registry state.
- Read-only semantics must be explicitly defined and tested.
- Rebuild/upgrade publication cannot transiently bypass admission.
Validation
Add cross-process tests on Windows, Linux and macOS for:
- Direct vs Direct;
- Direct vs Shared in both opening orders;
- many Shared processes;
- many Direct objects in one process sharing one lease;
- mixed local Shared/Direct attempts;
- last-reference release;
- process kill/crash releasing the lock;
- opening again immediately after owner death;
- canonical-path aliases;
- rebuild/replace handoff;
- permission/read-only directories;
- behavior on filesystems where the selected native locking primitive is unsupported or unreliable.
Unsupported locking environments should fail closed rather than silently degrading writable mode safety.
Success criteria
- #3023 no longer needs a persistent
<database>-shared-modeadmission file. - Mode admission has one clear process-lifetime owner/refcount model.
- Cross-process incompatibility is enforced by the OS wherever supported.
- Existing Shared mapped coordination remains independently versioned and can evolve separately.
Source: litedb-org/LiteDB