grafana/k6 · error
a function is expected as the second argument of SharedArray
Error message
a function is expected as the second argument of SharedArray's constructor
What it means
k6's SharedArray constructor takes (name, callback) where callback is a function that returns the array to share. Before storing the array, k6 asserts the second argument is callable via sobek.AssertFunction and throws this error if it is not. The callback defers materialization so only the first VU pays the construction cost and the result is shared as read-only data.
Source
Thrown at internal/js/modules/k6/data/data.go:93
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)
}
return array.wrap(rt).ToObject(rt)
}
// RecordReader is the interface that wraps the action of reading records from a resource.
//
// The data module RecordReader interface is implemented by types that can read data that can be
// treated as records, from data sources such as a CSV file, etc.
type RecordReader interface {
Read() (any, error)
}View on GitHub (pinned to 93accf6570)
Solutions
- Wrap the data in a closure: new SharedArray('name', () => myArray)
- Verify argument order: name string first, function second
- If the builder is dynamic, assert typeof builder === 'function' before constructing
Example fix
// before
const data = [1, 2, 3];
const s = new SharedArray('data', data);
// after
const data = [1, 2, 3];
const s = new SharedArray('data', () => data); Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof name !== 'string' || name === '') throw new Error('SharedArray name must be a non-empty string');
if (typeof builder !== 'function') throw new Error('SharedArray second argument must be a function');
new SharedArray(name, builder); Type guard
/** @param {unknown} v @returns {v is () => unknown[]} */
function isSharedArrayBuilder(v) {
return typeof v === 'function';
}
// usage: if (!isSharedArrayBuilder(builder)) throw new TypeError('builder must be a function'); Prevention
- Always write SharedArray construction as new SharedArray('name', () => data) -- the closure form is the only valid one
- Lint for the (name, array) pattern in code review; k6 cannot infer intent from a non-function value
- Keep the builder pure and synchronous; async functions are rejected with a separate error
When it happens
Trigger: Calling new SharedArray('name', [1,2,3]) with a bare array instead of a function; passing a string, plain object, or undefined as the second argument; passing a variable that was reassigned to a non-function before the constructor runs.
Common situations: Users migrating from open()/JSON.parse patterns assume the data itself is passed directly. Others call a helper that returns data but forget the wrapping closure, or accidentally invoke the function immediately and pass its (non-function) result as the second argument.
Related errors
- only arrays can be made into SharedArray
- Unexpected end of selector while parsing selector `${selecto
- Error while parsing selector `${selector}`: ${e.message}
- Error while parsing selector `${selector}` - cannot use ${op
- exported 'setup' must be a function
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/6919104f4a6f4f19.
Report an issue: GitHub.