{"record":{"id":"ba3532a885b2097d","repo":"XTLS/Xray-core","slug":"failed-to-load-code","errorCode":null,"errorMessage":"failed to load code ","messagePattern":"failed to load code ","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/geodata/geodat_loader.go","lineNumber":37,"sourceCode":"\t\treturn errors.New(\"failed to open \", file).Base(err)\n\t}\n\tdefer r.Close()\n\tif _, err := find(r, []byte(code), false); err != nil {\n\t\treturn errors.New(\"failed to check code \", code, \" from \", file).Base(err)\n\t}\n\treturn nil\n}\n\nfunc loadFile(file, code string) ([]byte, error) {\n\truntime.GC() // peak mem\n\tr, err := filesystem.OpenAsset(file)\n\tif err != nil {\n\t\treturn nil, errors.New(\"failed to open \", file).Base(err)\n\t}\n\tdefer r.Close()\n\tbs, err := find(r, []byte(code), true)\n\tif err != nil {\n\t\treturn nil, errors.New(\"failed to load code \", code, \" from \", file).Base(err)\n\t}\n\treturn bs, nil\n}\n\nfunc loadIP(file, code string) ([]*CIDR, error) {\n\tbs, err := loadFile(file, code)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tdefer runtime.GC() // peak mem\n\tvar geoip GeoIP\n\tif err := proto.Unmarshal(bs, &geoip); err != nil {\n\t\treturn nil, errors.New(\"error unmarshal IP in \", file, \":\", code).Base(err)\n\t}\n\treturn geoip.Cidr, nil\n}\n\nfunc loadSite(file, code string) ([]*Domain, error) {","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/XTLS/Xray-core/blob/7d214f8b094f75322fa3990f8aadad1c912f24f5/common/geodata/geodat_loader.go#L19-L55","documentation":"Wrapped error from loadFile when find(..., readBody=true) locates nothing it can read a body for: after the file is open, scanning the entry stream fails — code header never matched, stream truncated mid-entry, or a decode error (EOF/unexpected EOF propagates as the Base error). Distinguished from the open failure: the file exists but its content does not yield the requested code.","triggerScenarios":"loadFile('geoip.dat','cn') on a truncated or corrupted dat where the scanner hits EOF before matching the code, or the code is absent while earlier structure still parses partially.","commonSituations":"Interrupted downloads of dat files; dat files from mirrors that serve error pages (HTML) instead of protobuf; codes specific to extended lists missing from minimal dat files.","solutions":["Re-download the dat file from the official release and verify size/checksum, then restart.","Confirm the requested code exists in that dat (use the check path's error or a geosite listing tool).","Avoid mirrors/CDNs that may serve HTML error pages; pin to GitHub release assets."],"exampleFix":"# before\n$ xray run -c config.json # geosite.dat is a truncated 12KB HTML page\n\n# after\n$ curl -L -o /etc/xray/geosite.dat https://github.com/v2fly/domain-list-community/releases/latest/download/dlc.dat\n$ xray run -c config.json","handlingStrategy":"retry","validationCode":"// Verify dat integrity (size/checksum) before launch:\nif fi, _ := os.Stat(p); fi != nil && fi.Size() < 1024*100 { /* too small: re-provision */ }","typeGuard":null,"tryCatchPattern":"bs, err := loadFile(file, code)\nif err != nil && strings.Contains(err.Error(), \"failed to load code\") {\n    // re-download dat atomically, then retry once\n}","preventionTips":["Download dat files from official GitHub releases and verify hashes","Never serve HTML error pages from dat mirrors"],"tags":["geodata","corruption","download","routing","xray"],"backgroundTag":null,"analyzedSha":"7d214f8b094f75322fa3990f8aadad1c912f24f5","analyzedAt":"2026-08-15T14:26:24.325Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}