grafana/k6 · error
new SharedArray must be called in the init context
Error message
new SharedArray must be called in the init context
What it means
SharedArray must be constructed in the init context so its contents are serialized once and shared read-only across all VUs. data.go:76-80 throws when d.vu.State() != nil — meaning the constructor ran in the VU stage (inside export default, setup, or teardown) where cross-VU sharing is impossible.
Source
Thrown at internal/js/modules/k6/data/data.go:78
// Exports returns the exports of the data module.
func (d *Data) Exports() modules.Exports {
return modules.Exports{
Named: map[string]any{
"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) }View on GitHub (pinned to 93accf6570)
Solutions
- Move the SharedArray constructor to the top level of the script (init context)
- Keep only references to the array inside export default / setup / teardown
- If data depends on init-time computation, do it with open() and JSON.parse at top level, then wrap in SharedArray
Example fix
// before
export default function () {
const users = new SharedArray('users', () => [/* ... */]); // throws
// ...
}
// after
const users = new SharedArray('users', () => [/* ... */]); // init context
export default function () {
const u = users[Math.floor(Math.random() * users.length)];
} Defensive patterns
Strategy: validation
Validate before calling
// Structural check (static, not runtime): the constructor call must sit at module
// top level, outside export default / setup / teardown.
// Place data setup like this:
const data = JSON.parse(open('./data.json'));
const shared = new SharedArray('data', function () { return data; }); Prevention
- Keep all SharedArray constructors at the top level of the entry script
- Review refactors that move init code into default/setup functions
- Reference the array inside export default — never construct it there
When it happens
Trigger: Placing `new SharedArray(...)` inside export default function(), setup(), teardown(), or any function first invoked during the VU stage rather than at module top level.
Common situations: Refactors that move data loading into the default function; newcomers unfamiliar with k6's init-vs-VU stages; conditionally building arrays inside iteration code.
Related errors
- SharedArray constructor does not support async functions as
- empty name provided to SharedArray's constructor
- open() can't be used with an empty filename
- invalid size
- unsupported public key algorithm
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/22ac81ce6108c069.
Report an issue: GitHub.