Tencent/WeKnora · error

failed to copy file content: %w

Error message

failed to copy file content: %w

What it means

io.Copy(dst, src) failed while streaming the source file's contents into the destination file. This is a mid-copy I/O failure: read errors from the source (I/O error, file truncated concurrently), write errors to the destination (disk full), or network-filesystem interruptions.

Source

Thrown at internal/application/service/file/local.go:206

	ext := filepath.Ext(srcPath)
	filename := fmt.Sprintf("%d%s", time.Now().UnixNano(), ext)
	dstPath := filepath.Join(dir, filename)

	src, err := os.Open(srcResolved)
	if err != nil {
		return "", fmt.Errorf("failed to open source file: %w", err)
	}
	defer src.Close()

	dst, err := os.Create(dstPath)
	if err != nil {
		return "", fmt.Errorf("failed to create destination file: %w", err)
	}
	defer dst.Close()

	if _, err := io.Copy(dst, src); err != nil {
		return "", fmt.Errorf("failed to copy file content: %w", err)
	}

	relPath, _ := filepath.Rel(s.baseDir, dstPath)
	newPath := localScheme + filepath.ToSlash(relPath)
	logger.Infof(ctx, "Copied local file %s to %s", srcPath, newPath)
	return newPath, nil
}

// SaveBytes saves bytes data to a file and returns the file path
// temp parameter is ignored for local storage (no auto-expiration support)
// fileName 仅允许安全文件名,禁止路径遍历(如 ../../)
func (s *localFileService) SaveBytes(ctx context.Context, data []byte, tenantID uint64, fileName string, temp bool) (string, error) {
	logger.Infof(ctx, "Saving bytes data: fileName=%s, size=%d, tenantID=%d, temp=%v", fileName, len(data), tenantID, temp)

	safeName, err := secutils.SafeFileName(fileName)
	if err != nil {
		logger.Errorf(ctx, "Invalid fileName for SaveBytes: %v", err)
		return "", fmt.Errorf("invalid file name: %w", err)

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Inspect the wrapped error: ENOSPC mid-copy → free space and retry; EIO/stale NFS handle → check mount health and remount.
  2. Retry CopyFile after confirming source stability (no concurrent writers); consider copying to a temp file and atomically renaming on success.
  3. Check for concurrent modification of the source and serialize writes to that file.
  4. Monitor and expand volume capacity for large copies.

Example fix

// before
if _, err := io.Copy(dst, src); err != nil {
    return "", fmt.Errorf("failed to copy file content: %w", err)
}
// after
if _, err := io.Copy(dst, src); err != nil {
    dst.Close()
    os.Remove(dstPath)
    return "", fmt.Errorf("failed to copy file content: %w", err)
}
if err := dst.Sync(); err != nil {
    dst.Close()
    os.Remove(dstPath)
    return "", fmt.Errorf("failed to flush destination file: %w", err)
}
Defensive patterns

Strategy: retry

Validate before calling

func checkCopyPrereqs(srcPath, dir string, minFreeBytes uint64) error {
    if fi, err := os.Stat(srcPath); err != nil || !fi.Mode().IsRegular() {
        return fmt.Errorf("source invalid")
    }
    var st syscall.Statfs_t
    if err := syscall.Statfs(dir, &st); err != nil {
        return err
    }
    if uint64(st.Bavail)*uint64(st.Bsize) < minFreeBytes {
        return fmt.Errorf("insufficient free space")
    }
    return nil
}

Try / catch

var newPath string
var err error
for attempt := 0; attempt < 3; attempt++ {
    newPath, err = svc.CopyFile(ctx, srcPath, tenantID, knowledgeID)
    if err == nil {
        break
    }
    if errors.Is(err, os.ErrPermission) || errors.Is(err, fs.ErrNotExist) {
        break // non-transient: don't retry
    }
    time.Sleep(time.Duration(1<<attempt) * 100 * time.Millisecond)
}
if err != nil {
    return fmt.Errorf("copy failed after retries: %w", err)
}

Prevention

When it happens

Trigger: Source file is modified/truncated while being read (concurrent writer); disk fills during the copy; NFS/EFS/S3-fuse mount drops mid-read; source file is on flaky removable/network storage.

Common situations: Large files copied while another process rewrites the source; volume runs out of space partway through a big copy; unstable network mounts (NFS stale handle, EFS timeout) in cloud deployments.

Related errors


AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02). Data as JSON: /api/errors/caaf1161de0ffe03. Report an issue: GitHub.