gofiber/fiber · error

failed to copy file data: %w

Error message

failed to copy file data: %w

What it means

Returned in addFormFile (client/hooks.go:322) when io.CopyBuffer fails while copying the opened file's contents into the multipart form-file part. A 1MB pooled buffer is used for the copy. The error wraps either a read failure on the source file or a write failure on the multipart body writer.

Source

Thrown at client/hooks.go:322

	if f.reader == nil {
		var err error
		f.reader, err = os.Open(f.path)
		if err != nil {
			return fmt.Errorf("open file error: %w", err)
		}
	}

	// Ensure the file reader is always closed after copying.
	defer f.reader.Close() //nolint:errcheck // not needed

	// Create form file and copy the content.
	w, err := mw.CreateFormFile(f.fieldName, f.name)
	if err != nil {
		return fmt.Errorf("create file error: %w", err)
	}

	if _, err := io.CopyBuffer(w, f.reader, *fileBuf); err != nil {
		return fmt.Errorf("failed to copy file data: %w", err)
	}

	return nil
}

// parserResponseCookie parses the Set-Cookie headers from the response and stores them.
func parserResponseCookie(c *Client, resp *Response, req *Request) error {
	var err error
	for key, value := range resp.RawResponse.Header.Cookies() {
		cookie := fasthttp.AcquireCookie()
		if err = cookie.ParseBytes(value); err != nil {
			fasthttp.ReleaseCookie(cookie)
			break
		}
		cookie.SetKeyBytes(key)
		resp.cookie = append(resp.cookie, cookie)
	}

View on GitHub (pinned to 9a4c7e57fe)

Solutions

  1. Ensure the source file is fully readable for the duration of the copy (lock it, copy to a local tempfile first for network FS).
  2. Do not release/reuse the Request while the upload is in flight.
  3. Retry the upload if the cause was a transient read/write failure.
  4. For very large files, prefer a streaming body that does not require buffering the whole file.

Example fix

// before — reading directly from a flaky network mount
req.SetFiles("/mnt/nfs/large.dat")

// after — stage to local disk first, then upload
local, _ := os.CreateTemp("", "upload-")
src, _ := os.Open("/mnt/nfs/large.dat")
io.Copy(local, src); local.Close(); src.Close()
req.SetFiles(local.Name())
Defensive patterns

Strategy: retry

Try / catch

resp, err := core.execute(ctx, client, req)
if err != nil && strings.Contains(err.Error(), "failed to copy file data") {
    // transient read/write — re-stage and retry
}

Prevention

When it happens

Trigger: The source file becomes unreadable mid-copy (deleted, permission revoked, disk error, NFS hiccup), or the destination body writer fails (buffer cap, transport reset). Occurs after the file was opened and the part header written successfully.

Common situations: Network filesystem (NFS/CIFS) that drops mid-read; the file truncated/deleted during upload; concurrent release of the Request; uploading a file larger than the request body buffer without streaming; disk I/O error on the source.

Related errors


AI-assisted analysis of gofiber/fiber@9a4c7e57fe (2026-08-04). Data as JSON: /data/errors/b64ae50ed31fcd75.json. Report an issue: GitHub.