{"record":{"id":"93c5d3b64eb1a00d","repo":"dgraph-io/badger","slug":"unable-to-open-mem-dir","errorCode":null,"errorMessage":"Unable to open mem dir.","messagePattern":"Unable to open mem dir\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"memtable.go","lineNumber":50,"sourceCode":"// both to the WAL and the skiplist. On a crash, the WAL is replayed to bring the skiplist back to\n// its pre-crash form.\ntype memTable struct {\n\t// TODO: Give skiplist z.Calloc'd []byte.\n\tsl         *skl.Skiplist\n\twal        *logFile\n\tmaxVersion uint64\n\topt        Options\n\tbuf        *bytes.Buffer\n}\n\nfunc (db *DB) openMemTables(opt Options) error {\n\t// We don't need to open any tables in in-memory mode.\n\tif db.opt.InMemory {\n\t\treturn nil\n\t}\n\tfiles, err := os.ReadDir(db.opt.Dir)\n\tif err != nil {\n\t\treturn errFile(err, db.opt.Dir, \"Unable to open mem dir.\")\n\t}\n\n\tvar fids []int\n\tfor _, file := range files {\n\t\tif !strings.HasSuffix(file.Name(), memFileExt) {\n\t\t\tcontinue\n\t\t}\n\t\tfsz := len(file.Name())\n\t\tfid, err := strconv.ParseInt(file.Name()[:fsz-len(memFileExt)], 10, 64)\n\t\tif err != nil {\n\t\t\treturn errFile(err, file.Name(), \"Unable to parse log id.\")\n\t\t}\n\t\tfids = append(fids, int(fid))\n\t}\n\n\t// Sort in ascending order.\n\tsort.Slice(fids, func(i, j int) bool {\n\t\treturn fids[i] < fids[j]","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/memtable.go#L32-L68","documentation":"At DB open, badger lists the configured Options.Dir to discover memtable WAL files. This error wraps the os.ReadDir failure, so the memtable directory itself could not be read (missing, permission denied, not a directory, or I/O error). In-memory mode skips this entirely.","triggerScenarios":"Calling badger.Open (or OpenDB) with Options.Dir pointing to a path that does not exist, is not readable by the process, is a file instead of a directory, or lives on a failed/unmounted volume.","commonSituations":"Wrong path in config or env var; running in a container where the volume was not mounted; permission differences after running once as root then as a normal user; NFS/network mount outage.","solutions":["Verify the directory exists and create it if not: os.MkdirAll(dir, 0700) before badger.Open","Check process permissions: ls -ld <dir> and ensure the running user can read/execute it","Confirm the path is a directory, not a file, and that the volume is mounted (docker/k8s volume mounts)","If in-memory mode is intended, set Options.InMemory=true so no dir is required"],"exampleFix":"// before\nbadger.Open(badger.DefaultOptions(\"/data/badger\"))\n// after\nif err := os.MkdirAll(\"/data/badger\", 0o700); err != nil {\n    return err\n}\ndb, err := badger.Open(badger.DefaultOptions(\"/data/badger\"))","handlingStrategy":"validation","validationCode":"info, err := os.Stat(dir)\nif err != nil {\n    if os.IsNotExist(err) { os.MkdirAll(dir, 0o700) }\n} else if !info.IsDir() {\n    return fmt.Errorf(\"%s is not a directory\", dir)\n}","typeGuard":"func dirReadable(dir string) error {\n    fi, err := os.Stat(dir)\n    if err != nil { return err }\n    if !fi.IsDir() { return fmt.Errorf(\"not a directory: %s\", dir) }\n    f, err := os.Open(dir)\n    if err != nil { return err }\n    return f.Close()\n}","tryCatchPattern":"db, err := badger.Open(opt)\nif err != nil && strings.Contains(err.Error(), \"Unable to open mem dir\") {\n    // surface actionable guidance for perms/mount issues\n    return fmt.Errorf(\"check that %q exists, is a directory, and is readable by uid %d: %w\", opt.Dir, os.Getuid(), err)\n}","preventionTips":["Call os.MkdirAll on Options.Dir before opening","In containers, verify volume mounts at startup with a readiness check","Avoid running once as root then as an unprivileged user (permission drift)","Prefer Options.InMemory=true when no persistence is needed"],"tags":["badger","filesystem","permissions","startup"],"backgroundTag":"directory-not-accessible","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"}