{"id":"fd9b61a6587423ae","repo":"gofiber/fiber","slug":"failed-to-close-multipart-writer-w","errorCode":null,"errorMessage":"failed to close multipart writer: %w","messagePattern":"failed to close multipart writer: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"client/hooks.go","lineNumber":250,"sourceCode":"\treturn nil\n}\n\n// parserRequestBodyFile handles the case where the request contains files to be uploaded.\nfunc parserRequestBodyFile(req *Request) error {\n\tmw := multipart.NewWriter(req.RawRequest.BodyWriter())\n\tif err := mw.SetBoundary(req.boundary); err != nil {\n\t\treturn fmt.Errorf(\"set boundary error: %w\", err)\n\t}\n\n\tif err := writeMultipartBody(mw, req); err != nil {\n\t\tmw.Close() //nolint:errcheck // the body write already failed; surface that error instead\n\t\treturn err\n\t}\n\n\t// Close writes the trailing boundary; if it fails the multipart body is\n\t// incomplete, so surface the error instead of sending a malformed request.\n\tif err := mw.Close(); err != nil {\n\t\treturn fmt.Errorf(\"failed to close multipart writer: %w\", err)\n\t}\n\n\treturn nil\n}\n\n// writeMultipartBody writes the form fields and files of req to mw.\nfunc writeMultipartBody(mw *multipart.Writer, req *Request) error {\n\t// Add form data.\n\tvar err error\n\tfor key, value := range req.formData.All() {\n\t\terr = mw.WriteField(utils.UnsafeString(key), utils.UnsafeString(value))\n\t\tif err != nil {\n\t\t\tbreak\n\t\t}\n\t}\n\tif err != nil {\n\t\treturn fmt.Errorf(\"write formdata error: %w\", err)\n\t}","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/client/hooks.go#L232-L268","documentation":"Returned in parserRequestBodyFile (client/hooks.go:250) when multipart.Writer.Close fails. Close writes the trailing boundary marker that terminates a multipart body; if the underlying writer (req.RawRequest.BodyWriter()) errors while flushing that marker, the body is incomplete and the error is surfaced rather than sending a malformed request.","triggerScenarios":"The request's body writer returns an error while the closing boundary is written — e.g. an in-memory buffer that hit a size cap, a streaming body destination whose connection was already torn down, or a fasthttp request body buffer overflow. Occurs only on multipart file-upload requests.","commonSituations":"Uploading a very large multipart body that exceeds an internal buffer/size limit; a body-stream target that was closed early; concurrent release/reuse of the Request mid-upload; a transport reset between writing files and closing the writer.","solutions":["Verify the Request is not being concurrently released/reused while the upload is being built.","Check whether the total multipart body size exceeds any configured max body size and raise it or stream instead.","Retry the request — if the underlying cause was a transient transport teardown, a fresh request may succeed.","Inspect the wrapped error to distinguish a size-limit failure from a connection-level failure."],"exampleFix":"// before — reusing/releasing the request concurrently\nresp, err := core.execute(ctx, client, req)\nReleaseRequest(req) // may race the close\n\n// after — keep the request alive until execute returns, then release\nresp, err := core.execute(ctx, client, req)\nif resp != nil { ReleaseResponse(resp) }\nReleaseRequest(req)","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"resp, err := core.execute(ctx, client, req)\nif err != nil && strings.Contains(err.Error(), \"failed to close multipart writer\") {\n    // likely a transient buffer/transport issue — rebuild and retry once\n    req = rebuildUploadRequest()\n    resp, err = core.execute(ctx, client, req)\n}","preventionTips":["Do not release or reuse the Request object while the multipart body is being built.","Confirm the total multipart body fits configured size limits or stream it.","Inspect the wrapped error to separate size-cap failures from transport resets."],"tags":["client","multipart","body-writer","upload"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}