{"record":{"id":"bad06985c9cbca3c","repo":"chenhg5/cc-connect","slug":"content-range-total-mismatch-got-d-want-d","errorCode":null,"errorMessage":"Content-Range total mismatch: got %d want %d","messagePattern":"Content-Range total mismatch: got (.+?) want (.+?)","errorType":"http","errorClass":null,"httpStatus":206,"severity":"error","filePath":"platform/feishu/resource_download.go","lineNumber":340,"sourceCode":"\t\treq.Header.Set(\"Range\", fmt.Sprintf(\"bytes=%d-%d\", start, end))\n\n\t\tresp, err := p.resourceDownloadHTTP.Do(req)\n\t\tif err != nil {\n\t\t\tif isTransientError(err) && attempt < maxTransientRetries {\n\t\t\t\tlastErr = err\n\t\t\t\tslog.Debug(p.tag()+\": transient range chunk error; retrying\",\n\t\t\t\t\t\"attempt\", attempt, \"error\", err, \"start\", start, \"end\", end)\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\treturn nil, fmt.Errorf(\"range request: %w\", err)\n\t\t}\n\n\t\tif resp.StatusCode == http.StatusPartialContent {\n\t\t\tcr := resp.Header.Get(\"Content-Range\")\n\t\t\tif cr != \"\" {\n\t\t\t\tif got, ok := parseContentRangeTotal(cr); ok && got != expectedTotal {\n\t\t\t\t\t_ = resp.Body.Close()\n\t\t\t\t\treturn nil, fmt.Errorf(\"Content-Range total mismatch: got %d want %d\", got, expectedTotal)\n\t\t\t\t}\n\t\t\t}\n\t\t\tbody, err := io.ReadAll(io.LimitReader(resp.Body, end-start+1+1))\n\t\t\t_ = resp.Body.Close()\n\t\t\tif err != nil {\n\t\t\t\tif isTransientError(err) && attempt < maxTransientRetries {\n\t\t\t\t\tlastErr = err\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\treturn nil, fmt.Errorf(\"read range body: %w\", err)\n\t\t\t}\n\t\t\tif int64(len(body)) != end-start+1 {\n\t\t\t\treturn nil, fmt.Errorf(\"range body length %d != requested %d\", len(body), end-start+1)\n\t\t\t}\n\t\t\treturn body, nil\n\t\t}\n\n\t\t// Non-206 response: capture a snippet for diagnostics, then either","sourceCodeStart":322,"sourceCodeEnd":358,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/platform/feishu/resource_download.go#L322-L358","documentation":"resourceRangeChunk fetches a byte range of a file from Feishu's download API and validates that a 206 Partial Content response carries a Content-Range header whose total size matches the size obtained from the initial full metadata fetch. If the server reports a different total, the file changed mid-download or the range request hit an inconsistent replica, so the download is aborted to prevent producing a corrupt concatenated file.","triggerScenarios":"Calling resourceFetchRemainingChunks (multi-chunk resumable download) when a subsequent Range request returns 206 with a Content-Range total that differs from expectedTotal — i.e. the remote file's size changed between the metadata fetch and the chunk fetches, or parseContentRangeTotal parsed a total from a different (stale/cached) representation.","commonSituations":"File was edited/re-uploaded while a large (>8MiB chunked) download was in progress; Feishu drive served a cached older version of the file; concurrent writers updating the same drive file; passing a stale file metadata token from a previous metadata call.","solutions":["Retry the whole download from scratch so a fresh expectedTotal is obtained from the initial metadata fetch","Check for concurrent writers to the drive file and re-run the download after edits settle","Verify the file metadata (size/token) used to compute expectedTotal was fetched immediately before the chunked download","If reproducible with a static file, log the Content-Range header and report a Feishu API inconsistency"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// before download, confirm the file is not being modified\nmeta := fetchFileMeta(fileToken)\nif time.Since(meta.ModifiedTime) < time.Minute {\n    return fmt.Errorf(\"file %s recently modified; retry download later\", fileToken)\n}","typeGuard":null,"tryCatchPattern":"if err := downloadResource(fileToken); err != nil && strings.Contains(err.Error(), \"Content-Range total mismatch\") {\n    // file changed mid-download: restart from fresh metadata\n    return downloadResource(fileToken)\n}","preventionTips":["Avoid editing a drive file while a chunked download of it is in flight","Always fetch fresh file metadata immediately before starting a chunked download","Use immutable/released file versions for programmatic downloads","Log Content-Range totals on mismatch to detect caching layers"],"tags":["http","feishu","download","range-request"],"backgroundTag":"unexpected-response-shape","analyzedSha":"4000b2338aa6e850c99df54f8b0ed6ed7460b401","analyzedAt":"2026-09-06T11:45:09.575Z","contentChangedAt":"2026-09-06T11:45:09.575Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}