{"record":{"id":"42b17b2a90e2428a","repo":"dgraph-io/badger","slug":"cannot-acquire-directory-lock-on-q-another-proc-42b17b","errorCode":null,"errorMessage":"Cannot acquire directory lock on %q.  Another process is using this Badger database.","messagePattern":"Cannot acquire directory lock on %q\\.  Another process is using this Badger database\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"dir_unix.go","lineNumber":56,"sourceCode":"\t// chdir in the meantime.\n\tabsPidFilePath, err := filepath.Abs(filepath.Join(dirPath, pidFileName))\n\tif err != nil {\n\t\treturn nil, y.Wrapf(err, \"cannot get absolute path for pid lock file\")\n\t}\n\tf, err := os.Open(dirPath)\n\tif err != nil {\n\t\treturn nil, y.Wrapf(err, \"cannot open directory %q\", dirPath)\n\t}\n\topts := unix.LOCK_EX | unix.LOCK_NB\n\tif readOnly {\n\t\topts = unix.LOCK_SH | unix.LOCK_NB\n\t}\n\n\terr = unix.Flock(int(f.Fd()), opts)\n\tif err != nil {\n\t\tf.Close()\n\t\treturn nil, y.Wrapf(err,\n\t\t\t\"Cannot acquire directory lock on %q.  Another process is using this Badger database.\",\n\t\t\tdirPath)\n\t}\n\n\tif !readOnly {\n\t\t// Yes, we happily overwrite a pre-existing pid file.  We're the\n\t\t// only read-write badger process using this directory.\n\t\terr = os.WriteFile(absPidFilePath, []byte(fmt.Sprintf(\"%d\\n\", os.Getpid())), 0666)\n\t\tif err != nil {\n\t\t\tf.Close()\n\t\t\treturn nil, y.Wrapf(err,\n\t\t\t\t\"Cannot write pid file %q\", absPidFilePath)\n\t\t}\n\t}\n\treturn &directoryLockGuard{f, absPidFilePath, readOnly}, nil\n}\n\n// Release deletes the pid file and releases our lock on the directory.\nfunc (guard *directoryLockGuard) release() error {","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/dir_unix.go#L38-L74","documentation":"On Unix systems Badger locks the database directory with unix.Flock on the LOCK file. This error is wrapped when flock fails — typically because another process already holds the lock — preventing two Badger instances from opening the same directory simultaneously.","triggerScenarios":"badger.Open on a directory already opened by another process (read-write vs read-write, or conflicting read-only modes); a stale process still holding the flock; a second app instance started against the same data directory.","commonSituations":"Running two replicas of a service pointed at the same DB path, a crashed/zombie process that never released the lock, containers sharing a mounted volume, or opening the DB while badger.StreamDB/tooling (badger info) holds it.","solutions":["Stop the other process using the directory (check with lsof <dir>/LOCK or ps) before reopening.","Fix deployment/config so only one instance points at the data directory (or use Badger's replication/raft rather than shared disk).","If a previous process crashed, verify no live holder exists then retry; flock is released automatically when the holder dies.","If you only need read access, consider opening readOnly consistently, but never mix modes with a live read-write opener."],"exampleFix":"// before\ndb, err := badger.Open(badger.DefaultOptions(\"/data/badger\"))\n// FATAL: Cannot acquire directory lock on \"/data/badger\"...\n// after\n// ensure the other instance is stopped, then:\ndb, err := badger.Open(badger.DefaultOptions(\"/data/badger\"))\nif err != nil { log.Fatalf(\"db in use: %v\", err) }\ndefer db.Close()","handlingStrategy":"try-catch","validationCode":"// before opening: check no other holder\nif _, err := os.Stat(filepath.Join(dir, \"LOCK\")); err == nil {\n\t// run: lsof <dir>/LOCK  — or attempt a probe open readOnly\n}","typeGuard":null,"tryCatchPattern":"db, err := badger.Open(opts)\nif err != nil {\n\tif strings.Contains(err.Error(), \"Cannot acquire directory lock\") {\n\t\t// another live process owns the dir; back off and retry later\n\t\ttime.Sleep(retryInterval)\n\t\treturn retryOpen(opts)\n\t}\n\treturn err\n}","preventionTips":["Use process supervision so only one instance runs against a data dir","Detect stale holders with lsof on the LOCK file; flock releases when the holder dies","Avoid shared volumes mounted by multiple containers/replicas"],"tags":["go","badger","file-lock","unix","concurrency"],"backgroundTag":"database-directory-lock","analyzedSha":"2a001d466f6b71a917319a1db41f99860e16e269","analyzedAt":"2026-09-05T13:00:02.264Z","contentChangedAt":"2026-09-05T13:00:02.264Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}