grafana/k6 · error

empty name provided to SharedArray's constructor

Error message

empty name provided to SharedArray's constructor

What it means

SharedArray's first argument names the shared memory slot used to share the array's contents between VUs. An empty name is rejected at data.go:81-85 because it cannot serve as a stable identifier for the cache that deduplicates arrays across the init and VU stages.

Source

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

			"SharedArray": d.sharedArray,
		},
	}
}

const asyncFunctionNotSupportedMsg = "SharedArray constructor does not support async functions as second argument"

// sharedArray is a constructor returning a shareable read-only array
// indentified by the name and having their contents be whatever the call returns
func (d *Data) sharedArray(call sobek.ConstructorCall) *sobek.Object {
	rt := d.vu.Runtime()

	if d.vu.State() != nil {
		common.Throw(rt, errors.New("new SharedArray must be called in the init context"))
	}

	name := call.Argument(0).String()
	if name == "" {
		common.Throw(rt, errors.New("empty name provided to SharedArray's constructor"))
	}
	val := call.Argument(1)

	if common.IsAsyncFunction(rt, val) {
		common.Throw(rt, errors.New(asyncFunctionNotSupportedMsg))
	}

	fn, ok := sobek.AssertFunction(val)
	if !ok {
		common.Throw(rt, errors.New("a function is expected as the second argument of SharedArray's constructor"))
	}

	builder := func() (sharedArray, error) { return getSharedArrayFromCall(rt, fn) }
	array, err := d.shared.loadOrStore(name, builder)
	if err != nil {
		common.Throw(rt, err)
	}

View on GitHub (pinned to 93accf6570)

Solutions

  1. Pass a unique, non-empty name — conventionally the same as the variable holding the array
  2. Give every SharedArray in the script a distinct name so their cached contents don't collide
  3. Validate generated names before use and fail with a clear message

Example fix

// before
new SharedArray(names[i], () => data[i]); // names[i] is ''

// after
if (!names[i]) throw new Error(`SharedArray name at ${i} is empty`);
new SharedArray(names[i], () => data[i]);
Defensive patterns

Strategy: validation

Validate before calling

if (typeof name !== 'string' || name.trim() === '') {
  throw new Error(`SharedArray name must be a non-empty string, got: ${JSON.stringify(name)}`);
}
new SharedArray(name, fn);

Type guard

const isValidSharedArrayName = (n) => typeof n === 'string' && n.trim().length > 0;

Prevention

When it happens

Trigger: new SharedArray('', () => [...]); a name built from a variable that is undefined or evaluates to '' (e.g. derived from a file path or config key that is missing).

Common situations: Generated names from data-driven configs that come out empty; copy-paste placeholders never replaced; name computed from array contents that turn out empty.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/3306f68f1196756b. Report an issue: GitHub.