Repository navigation
Description
What happens
Nothing stops a second process from opening a file storage root another process is using. All of these start without a warning:
- two Python processes calling
fluree.connect("./data"), or twoconnect()calls on one directory in one process - two
fluree server runinstances on different ports in one project directory - a mix of the two: a Python process on a server's
.fluree/storage
To reproduce on 4.2.3, start fluree server run twice in one project directory, on ports 18091 and 18092, then write through one and read through both:
insert 1 via s1 → s1 sees [1] s2 sees []
insert 2 via s2 → s1 sees [1] s2 sees [1, 2]
insert 3 via s1 → s1 sees [1, 2, 3] s2 sees [1, 2]
The Python package (4.2.4b1) behaves the same way.
What holds and what doesn't:
- Commits are not lost. The nameservice head is published with a compare-and-swap under a file lock. A write that meets a newer head is staged again on top of it.
- Reads go stale. Each process serves its own cached state and is never told about the other's commits.
- Indexing and sweep are not coordinated. Each process runs its own background indexer for the same ledgers. The sweep is documented as single-process-only (Build lease in the nameservice, so the storage sweep is safe across processes #1635), and it can delete index files that another process's build is about to publish.
The one existing guard is the PID file that fluree server start writes (fluree-db-cli/src/commands/server.rs), and it has two gaps:
- Only
server startchecks it. Foregroundserver runwrites no PID file, and the Python package never looks for one. - It decides "already running" from whether that PID is alive. After a crash, if another process has reused the PID, a start is refused for no reason.
The Python package's README currently invites this setup in two places:
- Its Concurrency section says "Several processes may share one database directory."
- Its fork guidance tells gunicorn users to fork workers before Fluree is first used, which gives each worker its own engine on the same directory.
A Ledger.refresh() for the Python package (#2037, closed) would have made reads catch up across processes. It would only have hidden the problem, since the setup itself is unsupported.
Proposal
One process owns a file storage root, and sharing across processes goes through a server. Enforce that with an OS advisory lock, the way RocksDB's LOCK file, SQLite and LMDB do:
- An opt-in builder option, for example
FlureeBuilder::file(path).exclusive(). At build time it takes an exclusive try-lock (fs2, as the WAL already uses) on a lock file in the root, and holds the handle for the instance's lifetime. A second open fails immediately, with an error that names the holder and says to run a server to share the data. It has to be opt-in because a Raft cluster's payload store shares one root across nodes on purpose, with one WAL per owner under.fluree-wal/owners/. - The Python package turns it on for file connections (
fluree.connect(path)). - The standalone server turns it on for both
server runandserver start, and decides "already running" by the lock. The PID file stays only soserver stopandserver statuscan find the process.
No stale locks
The lock belongs to an open file handle; the lock file existing on disk doesn't mean anything. The kernel releases the lock when the process exits by any route: normal exit, crash, SIGKILL or an OOM kill. I checked this: a flock holder killed with SIGKILL leaves the file behind, and the next try-lock succeeds immediately.
Edge cases:
- fork. A forked child shares the parent's handle, so if the child outlives the parent it keeps the lock alive. The child should close the handle it inherited. Closing that copy doesn't release the parent's lock. The Python package already refuses engine use in a forked child.
- Filesystems without lock support (some SMB and FUSE mounts). Log a warning and open anyway, as the WAL's owner lock already does. On NFSv4, the lock of a crashed holder on another machine is released when its lease expires.
- A second open in the same process. Refuse it too, since it would still be two engines, with a message saying to reuse the existing connection.
- A hung holder. It keeps the lock, which is correct because it's still alive. Write its PID into the lock file so the error message can name it, but never use the PID to decide anything.
Scope
- The builder option and the lock helper.
- Python file connections take the lock, plus the fork handling. Tests: a second process is refused, and the lock is free immediately after its holder is killed with
SIGKILL. fluree server runandserver starttake the lock in place of the PID-file liveness check.- Docs:
- the Python README's Concurrency section and fork/gunicorn guidance: one process per directory, using threads or asyncio within it, or else a server;
- the server docs: one instance per storage root.
- Still to decide: whether one-shot CLI commands that open storage directly should take the lock as well. With it, they'd be refused while a server holds the root, instead of reading stale state.
- Out of scope:
- coordinating several processes on one root (Build lease in the nameservice, so the storage sweep is safe across processes #1635);
- a read-only open alongside a writer.
Activity