{"record":{"id":"b69b76e6749008c9","repo":"AdguardTeam/AdGuardHome","slug":"opening-database-w","errorCode":null,"errorMessage":"opening database: %w","messagePattern":"opening database: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/stats/stats.go","lineNumber":189,"sourceCode":"\t\tconfigModifier: conf.ConfigModifier,\n\t\tfilename:       conf.Filename,\n\n\t\tconfMu:            &sync.RWMutex{},\n\t\tignored:           conf.Ignored,\n\t\tshouldCountClient: conf.ShouldCountClient,\n\t\tlimit:             conf.Limit,\n\t\tenabled:           conf.Enabled,\n\t}\n\n\tif s.unitIDGen = newUnitID; conf.UnitID != nil {\n\t\ts.unitIDGen = conf.UnitID\n\t}\n\n\t// TODO(e.burkov):  Move the code below to the Start method.\n\n\terr = s.openDB()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"opening database: %w\", err)\n\t}\n\n\tvar udb *unitDB\n\tid := s.unitIDGen()\n\n\ttx, err := s.db.Load().Begin(true)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"opening a transaction: %w\", err)\n\t}\n\n\tdeleted := s.deleteOldUnits(tx, id-uint32(s.limit.Hours())-1)\n\tudb = s.loadUnitFromDB(tx, id)\n\n\terr = finishTxn(tx, deleted > 0)\n\tif err != nil {\n\t\ts.logger.Error(\"finishing transacation\", slogutil.KeyError, err)\n\t}\n","sourceCodeStart":171,"sourceCodeEnd":207,"githubUrl":"https://github.com/AdguardTeam/AdGuardHome/blob/b41aefbe51c8dde65e2c50f093996afa0502edf9/internal/stats/stats.go#L171-L207","documentation":"StatsCtx.New failed to open (or initialize) the underlying bbolt statistics database; s.openDB()'s error is wrapped as 'opening database'.","triggerScenarios":"Calling stats.New when the DB file cannot be created/opened: bad path, permission denied, disk full, or a corrupt/locked database file.","commonSituations":"Working directory not writable, stats file owned by another user after running as root then as unprivileged user, two instances sharing the same data dir, or leftover lock.","solutions":["Verify the stats data directory exists and is writable by the process user","Check for another running instance holding the bbolt file","Remove/rename a corrupt stats DB file if the data is disposable"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0o644); err != nil { return err }; f.Close()","typeGuard":null,"tryCatchPattern":"s, err := stats.New(conf)\nif err != nil { log.Printf(\"stats init failed: %v\", err); return err }","preventionTips":["Pre-create and chown the data directory","Run one instance per data dir","Monitor disk space"],"tags":["stats","database","bbolt","filesystem"],"backgroundTag":"database-open-failed","analyzedSha":"b41aefbe51c8dde65e2c50f093996afa0502edf9","analyzedAt":"2026-08-27T04:57:55.097Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}