{"record":{"id":"15ea5a81120c88bf","repo":"TechnitiumSoftware/DnsServer","slug":"cachezonemanager-format-is-invalid","errorCode":null,"errorMessage":"CacheZoneManager format is invalid.","messagePattern":"CacheZoneManager format is invalid\\.","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Dns/ZoneManagers/CacheZoneManager.cs","lineNumber":116,"sourceCode":"        }\n\n        #endregion\n\n        #region zone file\n\n        public void LoadCacheZoneFile()\n        {\n            string cacheZoneFile = Path.Combine(_dnsServer.ConfigFolder, \"cache.bin\");\n\n            if (!File.Exists(cacheZoneFile))\n                return;\n\n            _dnsServer.LogManager.Write(\"Loading DNS Cache from disk...\");\n\n            using (FileStream fS = new FileStream(cacheZoneFile, FileMode.Open, FileAccess.Read))\n            {\n                if (Encoding.ASCII.GetString(fS.ReadExactly(2)) != \"CZ\")\n                    throw new InvalidDataException(\"CacheZoneManager format is invalid.\");\n\n                BinaryReader bR = new BinaryReader(fS);\n\n                int version = bR.ReadByte();\n                switch (version)\n                {\n                    case 1:\n                        int addedEntries = 0;\n\n                        try\n                        {\n                            bool serveStale = _dnsServer.ServeStale;\n\n                            while (bR.BaseStream.Position < bR.BaseStream.Length)\n                            {\n                                CacheZone zone = CacheZone.ReadFrom(bR, serveStale);\n                                if (!zone.IsEmpty)\n                                {","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Dns/ZoneManagers/CacheZoneManager.cs#L98-L134","documentation":"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.","triggerScenarios":"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.","commonSituations":"Crash mid-save left a half-written cache.bin; wrong file copied into the config folder; disk corruption.","solutions":["Delete cache.bin (the cache is non-persistent/ephemeral and rebuilds from queries) and restart.","Restore from a backup only if serve-stale warmth matters; otherwise regenerating is preferable.","Ensure SaveCacheZoneFile completed before stopping the server to avoid truncated files."],"exampleFix":"// before: cache.bin header != 'CZ' -> cache load fails at startup\n// after: remove so it regenerates\n//   rm config/cache.bin   (cache repopulates as queries arrive)","handlingStrategy":"validation","validationCode":"static bool IsValidCacheFile(string path)\n{\n    using var fs = File.OpenRead(path);\n    Span<byte> hdr = stackalloc byte[2];\n    return fs.Read(hdr) == 2 && hdr[0] == (byte)'C' && hdr[1] == (byte)'Z';\n}","typeGuard":null,"tryCatchPattern":"try { cacheZoneManager.LoadCacheZoneFile(); }\ncatch (InvalidDataException ex) when (ex.Message.Contains(\"CacheZoneManager format\"))\n{ File.Delete(cachePath); cacheZoneManager.LoadCacheZoneFile(); }","preventionTips":["Check the 'CZ' header before loading; treat cache.bin as disposable.","Avoid crashes mid-save (clean shutdown) to prevent truncated cache files.","Never substitute another feature's .bin at the cache path.","Prefer regenerating the cache over restoring it."],"tags":["dns","cache","config-file","serialization","startup"],"backgroundTag":null,"analyzedSha":"d0484b6c1e7439cdc53d67d81e9c876cda2ad756","analyzedAt":"2026-08-13T22:57:35.508Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}