chenhg5/cc-connect · error

Content-Range total mismatch: got %d want %d

Error message

Content-Range total mismatch: got %d want %d

What it means

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.

Source

Thrown at platform/feishu/resource_download.go:340

		req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", start, end))

		resp, err := p.resourceDownloadHTTP.Do(req)
		if err != nil {
			if isTransientError(err) && attempt < maxTransientRetries {
				lastErr = err
				slog.Debug(p.tag()+": transient range chunk error; retrying",
					"attempt", attempt, "error", err, "start", start, "end", end)
				continue
			}
			return nil, fmt.Errorf("range request: %w", err)
		}

		if resp.StatusCode == http.StatusPartialContent {
			cr := resp.Header.Get("Content-Range")
			if cr != "" {
				if got, ok := parseContentRangeTotal(cr); ok && got != expectedTotal {
					_ = resp.Body.Close()
					return nil, fmt.Errorf("Content-Range total mismatch: got %d want %d", got, expectedTotal)
				}
			}
			body, err := io.ReadAll(io.LimitReader(resp.Body, end-start+1+1))
			_ = resp.Body.Close()
			if err != nil {
				if isTransientError(err) && attempt < maxTransientRetries {
					lastErr = err
					continue
				}
				return nil, fmt.Errorf("read range body: %w", err)
			}
			if int64(len(body)) != end-start+1 {
				return nil, fmt.Errorf("range body length %d != requested %d", len(body), end-start+1)
			}
			return body, nil
		}

		// Non-206 response: capture a snippet for diagnostics, then either

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Retry the whole download from scratch so a fresh expectedTotal is obtained from the initial metadata fetch
  2. Check for concurrent writers to the drive file and re-run the download after edits settle
  3. Verify the file metadata (size/token) used to compute expectedTotal was fetched immediately before the chunked download
  4. If reproducible with a static file, log the Content-Range header and report a Feishu API inconsistency
Defensive patterns

Strategy: validation

Validate before calling

// before download, confirm the file is not being modified
meta := fetchFileMeta(fileToken)
if time.Since(meta.ModifiedTime) < time.Minute {
    return fmt.Errorf("file %s recently modified; retry download later", fileToken)
}

Try / catch

if err := downloadResource(fileToken); err != nil && strings.Contains(err.Error(), "Content-Range total mismatch") {
    // file changed mid-download: restart from fresh metadata
    return downloadResource(fileToken)
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/bad06985c9cbca3c. Report an issue: GitHub.