{"record":{"id":"3306f68f1196756b","repo":"grafana/k6","slug":"empty-name-provided-to-sharedarray-s-constructor","errorCode":null,"errorMessage":"empty name provided to SharedArray's constructor","messagePattern":"empty name provided to SharedArray's constructor","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/data/data.go","lineNumber":83,"sourceCode":"\t\t\t\"SharedArray\": d.sharedArray,\n\t\t},\n\t}\n}\n\nconst asyncFunctionNotSupportedMsg = \"SharedArray constructor does not support async functions as second argument\"\n\n// sharedArray is a constructor returning a shareable read-only array\n// indentified by the name and having their contents be whatever the call returns\nfunc (d *Data) sharedArray(call sobek.ConstructorCall) *sobek.Object {\n\trt := d.vu.Runtime()\n\n\tif d.vu.State() != nil {\n\t\tcommon.Throw(rt, errors.New(\"new SharedArray must be called in the init context\"))\n\t}\n\n\tname := call.Argument(0).String()\n\tif name == \"\" {\n\t\tcommon.Throw(rt, errors.New(\"empty name provided to SharedArray's constructor\"))\n\t}\n\tval := call.Argument(1)\n\n\tif common.IsAsyncFunction(rt, val) {\n\t\tcommon.Throw(rt, errors.New(asyncFunctionNotSupportedMsg))\n\t}\n\n\tfn, ok := sobek.AssertFunction(val)\n\tif !ok {\n\t\tcommon.Throw(rt, errors.New(\"a function is expected as the second argument of SharedArray's constructor\"))\n\t}\n\n\tbuilder := func() (sharedArray, error) { return getSharedArrayFromCall(rt, fn) }\n\tarray, err := d.shared.loadOrStore(name, builder)\n\tif err != nil {\n\t\tcommon.Throw(rt, err)\n\t}\n","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/data/data.go#L65-L101","documentation":"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.","triggerScenarios":"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).","commonSituations":"Generated names from data-driven configs that come out empty; copy-paste placeholders never replaced; name computed from array contents that turn out empty.","solutions":["Pass a unique, non-empty name — conventionally the same as the variable holding the array","Give every SharedArray in the script a distinct name so their cached contents don't collide","Validate generated names before use and fail with a clear message"],"exampleFix":"// before\nnew SharedArray(names[i], () => data[i]); // names[i] is ''\n\n// after\nif (!names[i]) throw new Error(`SharedArray name at ${i} is empty`);\nnew SharedArray(names[i], () => data[i]);","handlingStrategy":"validation","validationCode":"if (typeof name !== 'string' || name.trim() === '') {\n  throw new Error(`SharedArray name must be a non-empty string, got: ${JSON.stringify(name)}`);\n}\nnew SharedArray(name, fn);","typeGuard":"const isValidSharedArrayName = (n) => typeof n === 'string' && n.trim().length > 0;","tryCatchPattern":null,"preventionTips":["Name every SharedArray explicitly, ideally matching its variable name","Use distinct names per array to avoid cache collisions","Validate generated names from config/data before constructing"],"tags":["shared-array","validation","k6"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}