{"id":"b64ae50ed31fcd75","repo":"gofiber/fiber","slug":"failed-to-copy-file-data-w","errorCode":null,"errorMessage":"failed to copy file data: %w","messagePattern":"failed to copy file data: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"client/hooks.go","lineNumber":322,"sourceCode":"\tif f.reader == nil {\n\t\tvar err error\n\t\tf.reader, err = os.Open(f.path)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"open file error: %w\", err)\n\t\t}\n\t}\n\n\t// Ensure the file reader is always closed after copying.\n\tdefer f.reader.Close() //nolint:errcheck // not needed\n\n\t// Create form file and copy the content.\n\tw, err := mw.CreateFormFile(f.fieldName, f.name)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"create file error: %w\", err)\n\t}\n\n\tif _, err := io.CopyBuffer(w, f.reader, *fileBuf); err != nil {\n\t\treturn fmt.Errorf(\"failed to copy file data: %w\", err)\n\t}\n\n\treturn nil\n}\n\n// parserResponseCookie parses the Set-Cookie headers from the response and stores them.\nfunc parserResponseCookie(c *Client, resp *Response, req *Request) error {\n\tvar err error\n\tfor key, value := range resp.RawResponse.Header.Cookies() {\n\t\tcookie := fasthttp.AcquireCookie()\n\t\tif err = cookie.ParseBytes(value); err != nil {\n\t\t\tfasthttp.ReleaseCookie(cookie)\n\t\t\tbreak\n\t\t}\n\t\tcookie.SetKeyBytes(key)\n\t\tresp.cookie = append(resp.cookie, cookie)\n\t}\n","sourceCodeStart":304,"sourceCodeEnd":340,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/client/hooks.go#L304-L340","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the source file is fully readable for the duration of the copy (lock it, copy to a local tempfile first for network FS).","Do not release/reuse the Request while the upload is in flight.","Retry the upload if the cause was a transient read/write failure.","For very large files, prefer a streaming body that does not require buffering the whole file."],"exampleFix":"// before — reading directly from a flaky network mount\nreq.SetFiles(\"/mnt/nfs/large.dat\")\n\n// after — stage to local disk first, then upload\nlocal, _ := os.CreateTemp(\"\", \"upload-\")\nsrc, _ := os.Open(\"/mnt/nfs/large.dat\")\nio.Copy(local, src); local.Close(); src.Close()\nreq.SetFiles(local.Name())","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"resp, err := core.execute(ctx, client, req)\nif err != nil && strings.Contains(err.Error(), \"failed to copy file data\") {\n    // transient read/write — re-stage and retry\n}","preventionTips":["Stage network-mounted files to local disk before uploading.","Keep the source file open/readable for the whole copy duration.","Do not release the Request concurrently with the upload.","Retry on transient I/O errors after re-opening the source."],"tags":["client","upload","io-copy","filesystem"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}