{"record":{"id":"76c0818df8a9e4cb","repo":"dgraph-io/badger","slug":"cannot-acquire-directory-lock-on-q-another-proc","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_aix.go","lineNumber":66,"sourceCode":"func acquireDirectoryLock(dirPath string, pidFileName string, readOnly bool) (\n\t*directoryLockGuard, error) {\n\n\t// Convert to absolute path so that Release still works even if we do an unbalanced\n\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\n\taixFlockMapLock.Lock()\n\tdefer aixFlockMapLock.Unlock()\n\n\tlg := &directoryLockGuard{absPidFilePath, readOnly}\n\n\tif lock, fnd := aixFlockMap[absPidFilePath]; fnd {\n\t\tif !readOnly || lock.readOnly != readOnly {\n\t\t\treturn nil, fmt.Errorf(\n\t\t\t\t\"Cannot acquire directory lock on %q.  Another process is using this Badger database.\", dirPath)\n\t\t}\n\t\tlock.count++\n\t} else {\n\t\t// This is the first acquirer, set up a lock file and register it.\n\t\tf, err := os.OpenFile(absPidFilePath, os.O_RDWR|os.O_CREATE, 0666)\n\t\tif err != nil {\n\t\t\treturn nil, y.Wrapf(err, \"cannot create/open pid file %q\", absPidFilePath)\n\t\t}\n\n\t\topts := unix.F_WRLCK\n\t\tif readOnly {\n\t\t\topts = unix.F_RDLCK\n\t\t}\n\n\t\tflckt := unix.Flock_t{int16(opts), 0, 0, 0, 0, 0, 0}\n\t\terr = unix.FcntlFlock(uintptr(f.Fd()), unix.F_SETLK, &flckt)\n\t\tif err != nil {\n\t\t\tf.Close()","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/dir_aix.go#L48-L84","documentation":"On AIX, Badger's directory lock uses an in-process flock map keyed by the PID file path. This error is returned when the same process tries to acquire the directory lock a second time with incompatible modes (e.g. a read-write lock while a lock already exists, or read-only when an existing lock is read-write), which Badger treats as 'another process is using this database'.","triggerScenarios":"Opening two Badger DB instances on the same directory within one process (badger.Open called twice on the same path), or mixing a readOnly open with a read-write open on the same dir in the same process.","commonSituations":"Tests or services that accidentally reopen the DB without closing the first handle; a read-only background job opened while the main app holds a read-write lock; leaked DB handles after panics without db.Close().","solutions":["Close the existing Badger DB (db.Close()) before opening the same directory again.","Reuse a single *DB instance (singleton) instead of calling badger.Open repeatedly on the same path.","Ensure lock modes are consistent: don't open the same directory read-only while it is already open read-write (or vice versa).","Use defer db.Close() so panics/early returns don't leak the lock registration in aixFlockMap."],"exampleFix":"// before\ndb1, _ := badger.Open(opts)\ndb2, _ := badger.Open(opts) // Cannot acquire directory lock ...\n// after\ndb1, _ := badger.Open(opts)\ndefer db1.Close()\n// reuse db1 instead of opening db2","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"db, err := badger.Open(opts)\nif err != nil && strings.Contains(err.Error(), \"Cannot acquire directory lock\") {\n\tlog.Fatalf(\"badger dir already in use by this process: %v\", err)\n}","preventionTips":["One *badger.DB per directory per process (singleton pattern)","Always defer db.Close()","Don't mix readOnly and read-write opens on the same path"],"tags":["go","badger","file-lock","aix","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"}