{"record":{"id":"3c2531e645dcad6a","repo":"subhra74/xdm","slug":"unable-to-read-state-downloadstatestore","errorCode":null,"errorMessage":"Unable to read state: ","messagePattern":"Unable to read state: ","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"app/XDM/XDM.Core/IO/DownloadStateStore.cs","lineNumber":72,"sourceCode":"#if NET35\r\n                ms.CopyTo(stream);\r\n#endif\r\n            });\r\n        }\r\n    }\r\n\r\n    public static class DownloadStateStore\r\n    {\r\n        public static SingleSourceHTTPDownloaderState LoadSingleSourceHTTPDownloaderState(string id)\r\n        {\r\n            SingleSourceHTTPDownloaderState? state = null;\r\n            TransactedBinaryDataReader.Read($\"{id}.state\", Config.DataDir, r =>\r\n            {\r\n                state = SingleSourceHTTPDownloaderStateFromBytes(r);\r\n            });\r\n            if (state == null)\r\n            {\r\n                throw new IOException(\"Unable to read state: \" + id);\r\n            }\r\n            return state;\r\n        }\r\n\r\n        private static SingleSourceHTTPDownloaderState SingleSourceHTTPDownloaderStateFromBytes(BinaryReader r)\r\n        {\r\n            var state = new SingleSourceHTTPDownloaderState\r\n            {\r\n                Id = r.ReadString(),\r\n                TempDir = XDM.Messaging.StreamHelper.ReadString(r),\r\n                FileSize = r.ReadInt64(),\r\n                LastModified = DateTime.FromBinary(r.ReadInt64()),\r\n                SpeedLimit = r.ReadInt32(),\r\n                Url = new Uri(r.ReadString())\r\n            };\r\n            if (r.ReadBoolean())\r\n            {\r\n                XDM.Messaging.StreamHelper.ReadStateHeaders(r, out Dictionary<string, List<string>> headers);\r","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/subhra74/xdm/blob/1ca5a25aae007826c859c81bea494e7c102e1242/app/XDM/XDM.Core/IO/DownloadStateStore.cs#L54-L90","documentation":"DownloadStateStore.LoadSingleSourceHTTPDownloaderState throws IOException(\"Unable to read state: <id>\") when the transacted read of <id>.state leaves the state variable null after SingleSourceHTTPDownloaderStateFromBytes runs. This is the DownloadStateStore twin of the DownloadStateIO loader: it enforces that a stored state file for a single-source HTTP download either loads completely or throws.","triggerScenarios":"Calling DownloadStateStore.LoadSingleSourceHTTPDownloaderState(id) with a corrupt, empty, truncated, or format-incompatible <id>.state file in Config.DataDir, causing the byte-level deserializer to return null.","commonSituations":"Crash or forced termination between state-write and commit; XDM upgrade changing the state binary layout; disk corruption; users manually restoring/pasting state files from other installs.","solutions":["Delete the offending <id>.state from Config.DataDir and restart the download.","Restore the state file from backup, matching the XDM version that wrote it.","Check for orphaned transaction temp files next to the state file and clean/restore as appropriate.","If recurring, verify disk health and free space on the volume holding Config.DataDir."],"exampleFix":"// before\nvar state = DownloadStateStore.LoadSingleSourceHTTPDownloaderState(id); // throws\n// after\nSingleSourceHTTPDownloaderState state;\ntry { state = DownloadStateStore.LoadSingleSourceHTTPDownloaderState(id); }\ncatch (IOException) { DownloadStateStore.DeleteState(id); state = null; /* start fresh */ }","handlingStrategy":"try-catch","validationCode":"var path = Path.Combine(Config.DataDir, id + \".state\");\nif (!File.Exists(path) || new FileInfo(path).Length == 0)\n{\n    // start a fresh download instead of calling Load\n}","typeGuard":"static bool StateFileLooksValid(string dataDir, string id)\n{\n    var p = Path.Combine(dataDir, id + \".state\");\n    try { return File.Exists(p) && new FileInfo(p).Length > 0; }\n    catch (IOException) { return false; }\n}","tryCatchPattern":"SingleSourceHTTPDownloaderState state;\ntry { state = DownloadStateStore.LoadSingleSourceHTTPDownloaderState(id); }\ncatch (IOException)\n{\n    DownloadStateStore.DeleteState(id);\n    state = null; // restart download\n}","preventionTips":["Commit state writes gracefully — avoid abrupt terminations.","Keep read and write versions of the state format in sync.","Backup the state directory before version upgrades.","Check for leftover transaction files and clean them after crashes."],"tags":["io","state-persistence","deserialization","corrupt-file"],"backgroundTag":"file-read-failed","analyzedSha":"1ca5a25aae007826c859c81bea494e7c102e1242","analyzedAt":"2026-09-13T20:37:40.968Z","contentChangedAt":"2026-09-13T20:37:40.968Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}