{"record":{"id":"6919104f4a6f4f19","repo":"grafana/k6","slug":"a-function-is-expected-as-the-second-argument-of-s","errorCode":null,"errorMessage":"a function is expected as the second argument of SharedArray's constructor","messagePattern":"a function is expected as the second argument of SharedArray's constructor","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/data/data.go","lineNumber":93,"sourceCode":"\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\n\treturn array.wrap(rt).ToObject(rt)\n}\n\n// RecordReader is the interface that wraps the action of reading records from a resource.\n//\n// The data module RecordReader interface is implemented by types that can read data that can be\n// treated as records, from data sources such as a CSV file, etc.\ntype RecordReader interface {\n\tRead() (any, error)\n}","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/data/data.go#L75-L111","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nconst data = [1, 2, 3];\nconst s = new SharedArray('data', data);\n\n// after\nconst data = [1, 2, 3];\nconst s = new SharedArray('data', () => data);","handlingStrategy":"type-guard","validationCode":"if (typeof name !== 'string' || name === '') throw new Error('SharedArray name must be a non-empty string');\nif (typeof builder !== 'function') throw new Error('SharedArray second argument must be a function');\nnew SharedArray(name, builder);","typeGuard":"/** @param {unknown} v @returns {v is () => unknown[]} */\nfunction isSharedArrayBuilder(v) {\n  return typeof v === 'function';\n}\n\n// usage: if (!isSharedArrayBuilder(builder)) throw new TypeError('builder must be a function');","tryCatchPattern":null,"preventionTips":["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"],"tags":["sharedarray","data","javascript","type-validation"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}