grafana/k6 · error

closing multipart form writer: %w

Error message

closing multipart form writer: %w

What it means

Raised in newFileUploadRequest when multipart.Writer.Close fails after all fields and the file part were written. Close writes the final multipart boundary; failure is a buffer-level write error (e.g. allocation failure or an already-corrupted writer state). It means the assembled form body is incomplete and the upload request cannot be finalized.

Source

Thrown at internal/js/modules/k6/browser/storage/file_persister.go:229

	// copy all form fields received from a presigned URL
	// response to the multipart form fields.
	var form bytes.Buffer
	fw := multipart.NewWriter(&form)
	for k, v := range psu.FormFields {
		if err := fw.WriteField(k, v); err != nil {
			return nil, fmt.Errorf("writing form field key %q and value %q: %w", k, v, err)
		}
	}
	// attach the file data to the form.
	ff, err := fw.CreateFormFile("file", psu.Name)
	if err != nil {
		return nil, fmt.Errorf("creating multipart form file: %w", err)
	}
	if _, err := io.Copy(ff, data); err != nil {
		return nil, fmt.Errorf("copying file data to multipart form: %w", err)
	}
	if err := fw.Close(); err != nil {
		return nil, fmt.Errorf("closing multipart form writer: %w", err)
	}

	req, err := http.NewRequestWithContext(
		ctx,
		psu.Method,
		psu.PresignedURL,
		&form,
	)
	if err != nil {
		return nil, fmt.Errorf("creating new request: %w", err)
	}
	req.Header.Set("Content-Type", fw.FormDataContentType())

	return req, nil
}

func checkStatusCode(resp *http.Response) error {
	if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {

View on GitHub (pinned to 01ffac6f24)

Solutions

  1. Retry the persist; transient buffer write failures are rare but recoverable
  2. Check process memory if form assembly failures recur with large artifacts
  3. Report as a bug if reproducible with specific artifact sizes
Defensive patterns

Strategy: retry

When it happens

Trigger: Thrown at internal/js/modules/k6/browser/storage/file_persister.go:229 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/82184d5e995d08b9. Report an issue: GitHub.