TechnitiumSoftware/DnsServer · error · InvalidDataException

CacheZoneManager format is invalid.

Error message

CacheZoneManager format is invalid.

What it means

Thrown by LoadCacheZoneFile when the cache.bin file's first 2 bytes are not the ASCII magic 'CZ'. The loader checks the format header before the version byte; any other content is treated as an invalid cache format. The cache file is optional (the method returns early if it does not exist), so this only fires when a non-empty, wrong-format file is present.

Source

Thrown at DnsServerCore/Dns/ZoneManagers/CacheZoneManager.cs:116

        }

        #endregion

        #region zone file

        public void LoadCacheZoneFile()
        {
            string cacheZoneFile = Path.Combine(_dnsServer.ConfigFolder, "cache.bin");

            if (!File.Exists(cacheZoneFile))
                return;

            _dnsServer.LogManager.Write("Loading DNS Cache from disk...");

            using (FileStream fS = new FileStream(cacheZoneFile, FileMode.Open, FileAccess.Read))
            {
                if (Encoding.ASCII.GetString(fS.ReadExactly(2)) != "CZ")
                    throw new InvalidDataException("CacheZoneManager format is invalid.");

                BinaryReader bR = new BinaryReader(fS);

                int version = bR.ReadByte();
                switch (version)
                {
                    case 1:
                        int addedEntries = 0;

                        try
                        {
                            bool serveStale = _dnsServer.ServeStale;

                            while (bR.BaseStream.Position < bR.BaseStream.Length)
                            {
                                CacheZone zone = CacheZone.ReadFrom(bR, serveStale);
                                if (!zone.IsEmpty)
                                {

View on GitHub (pinned to d0484b6c1e)

Solutions

  1. Delete cache.bin (the cache is non-persistent/ephemeral and rebuilds from queries) and restart.
  2. Restore from a backup only if serve-stale warmth matters; otherwise regenerating is preferable.
  3. Ensure SaveCacheZoneFile completed before stopping the server to avoid truncated files.

Example fix

// before: cache.bin header != 'CZ' -> cache load fails at startup
// after: remove so it regenerates
//   rm config/cache.bin   (cache repopulates as queries arrive)
Defensive patterns

Strategy: validation

Validate before calling

static bool IsValidCacheFile(string path)
{
    using var fs = File.OpenRead(path);
    Span<byte> hdr = stackalloc byte[2];
    return fs.Read(hdr) == 2 && hdr[0] == (byte)'C' && hdr[1] == (byte)'Z';
}

Try / catch

try { cacheZoneManager.LoadCacheZoneFile(); }
catch (InvalidDataException ex) when (ex.Message.Contains("CacheZoneManager format"))
{ File.Delete(cachePath); cacheZoneManager.LoadCacheZoneFile(); }

Prevention

When it happens

Trigger: A non-cache file placed at <ConfigFolder>/cache.bin; truncated/partial cache file from a crash during SaveCacheZoneFile; a file from another feature at that path.

Common situations: Crash mid-save left a half-written cache.bin; wrong file copied into the config folder; disk corruption.

Related errors


AI-assisted analysis of TechnitiumSoftware/DnsServer@d0484b6c1e (2026-08-13). Data as JSON: /api/errors/15ea5a81120c88bf. Report an issue: GitHub.