grafana/k6 · error

failed to marshal record; reason

Error message

failed to marshal record; reason: %w

What it means

Raised inside the SharedArray builder when json.Marshal fails on a single record read from the RecordReader ('%w' carries the marshal error). Records must be JSON-serializable to be stored as strings in the shared array; this fires when a record contains types Go cannot marshal (channels, functions, NaN floats, cyclic structures) — a data-shape problem in the record itself, not a read failure (reads have their own wrap).

Solutions

  1. Ensure every record contains only JSON-serializable values (strings, numbers, booleans, plain objects/arrays)
  2. Replace NaN/Infinity floats with null or concrete numbers
  3. Avoid exotic types in the reader's output records
  4. Inspect the wrapped marshal error to identify the offending field
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at internal/js/modules/k6/data/data.go:148 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/2c0e6bb3e2590a66. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/data/data.go:148

	// The builder function is passed to loadOrStore, which ensures it is only
	// executed once for a given name. This guarantees the underlying data is
	// initialized exactly once, even if multiple VUs call this function concurrently
	// with the same name.
	builder := func() (sharedArray, error) {
		var arr []string
		for {
			record, err := r.Read()
			if errors.Is(err, io.EOF) {
				break
			}
			if err != nil {
				return sharedArray{}, fmt.Errorf("failed to read record; reason: %w", err)
			}

			marshaled, err := json.Marshal(record)
			if err != nil {
				return sharedArray{}, fmt.Errorf("failed to marshal record; reason: %w", err)
			}

			arr = append(arr, string(marshaled))
		}

		return sharedArray{arr: arr}, nil
	}

	array, err := d.shared.loadOrStore(name, builder)
	if err != nil {
		return nil, err
	}

	return func() *sobek.Object {
		return array.wrap(rt).ToObject(rt)
	}, nil
}

View on GitHub (pinned to 01ffac6f24)