Fix multiple instances running from same dataDir (#2647)

* Fix multiple instances running from same dataDir

* Add exclusive lock on lock file

* Unlock lock file on process exit

* Fix minor issues in lock file implementation
This commit is contained in:
Bhaskar Metiya 2024-09-26 13:39:34 +05:30 committed by GitHub
parent 5e3f3db165
commit b0a4a9ef84
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 25 additions and 2 deletions

View File

@ -8,7 +8,7 @@
{.push raises: [].}
import
std/[os, enumutils],
std/[os, enumutils, exitprocs],
confutils,
confutils/std/net,
chronicles,
@ -60,7 +60,30 @@ proc run(
if pathExists.isErr():
fatal "Failed to create data directory",
dataDir = config.dataDir, error = pathExists.error
quit 1
quit QuitFailure
# Make sure multiple instances to the same dataDir do not exist
let
lockFilePath = config.dataDir.string / "fluffy.lock"
lockFlags = {OpenFlags.Create, OpenFlags.Read, OpenFlags.Write}
lockFileHandleResult = openFile(lockFilePath, lockFlags)
if lockFileHandleResult.isErr():
fatal "Failed to open lock file", error = ioErrorMsg(lockFileHandleResult.error)
quit QuitFailure
let lockFileHandle = lockFile(lockFileHandleResult.value(), LockType.Exclusive)
if lockFileHandle.isErr():
fatal "Please ensure no other fluffy instances are running with the same data directory",
dataDir = config.dataDir
quit QuitFailure
let lockFileIoHandle = lockFileHandle.value()
addExitProc(
proc() =
discard unlockFile(lockFileIoHandle)
discard closeFile(lockFileIoHandle.handle)
)
## Network configuration
let