AdguardTeam/AdGuardHome · error

encoding unit: %w

Error message

encoding unit: %w

What it means

Encoding the unitDB with gob before storing it in bbolt failed; wrapped as 'encoding unit'.

Source

Thrown at internal/stats/unit.go:354

		addr := s.Address
		u.upstreamsResponses[addr]++
		u.upstreamsTimeSum[addr] += uint64(s.QueryDuration.Microseconds())
	}
}

// flushUnitToDB puts udb to the database at id.
func (s *StatsCtx) flushUnitToDB(udb *unitDB, tx *bbolt.Tx, id uint32) (err error) {
	s.logger.Debug("flushing unit", "id", id, "req_num", udb.NTotal)

	bkt, err := tx.CreateBucketIfNotExists(idToUnitName(id))
	if err != nil {
		return fmt.Errorf("creating bucket: %w", err)
	}

	buf := &bytes.Buffer{}
	err = gob.NewEncoder(buf).Encode(udb)
	if err != nil {
		return fmt.Errorf("encoding unit: %w", err)
	}

	err = bkt.Put([]byte{0}, buf.Bytes())
	if err != nil {
		return fmt.Errorf("putting unit to database: %w", err)
	}

	return nil
}

func convertTopSlice(a []countPair) (m []map[string]uint64) {
	m = make([]map[string]uint64, 0, len(a))
	for _, it := range a {
		m = append(m, map[string]uint64{it.Name: it.Count})
	}

	return m
}

View on GitHub (pinned to b41aefbe51)

Solutions

  1. Check for recent changes to unitDB types (new unsupported fields)
  2. Ensure units are not mutated concurrently with flush
  3. Upgrade to a version where the encoding matches stored data
Defensive patterns

Strategy: try-catch

Try / catch

if err := s.Close(); err != nil { if strings.Contains(err.Error(), "encoding") { /* data shape issue; consider dropping stats DB */ } }

Prevention

When it happens

Trigger: flushUnitToDB serializing a unitDB containing a type that gob cannot encode (unexported fields without GobEncode, unsupported map key types, channels/funcs) or a nil embedded value.

Common situations: Struct changes between versions making gob encoding fail; internal state corrupted by concurrent mutation without proper locking.

Related errors


AI-assisted analysis of AdguardTeam/AdGuardHome@b41aefbe51 (2026-08-27). Data as JSON: /api/errors/4ff16b56fb666670. Report an issue: GitHub.