{"record":{"id":"62d03583a18e7dc4","repo":"grafana/k6","slug":"initializing-the-main-module-hasn-t-finished-this","errorCode":null,"errorMessage":"initializing the main module hasn't finished, this is a bug in k6 please report it","messagePattern":"initializing the main module hasn't finished, this is a bug in k6 please report it","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/bundle.go","lineNumber":375,"sourceCode":"\t\tvar err error\n\t\tresult, err = modSys.RunSourceData(b.sourceData)\n\t\treturn err\n\t}\n\n\tcall, _ := sobek.AssertFunction(vuImpl.runtime.ToValue(callback))\n\n\terr = vuImpl.eventLoop.Start(func() error {\n\t\t_, err := call(nil)\n\t\treturn err\n\t})\n\n\t<-initDone\n\n\tif err == nil {\n\t\tvar finished bool\n\t\tbi.mainModule, finished, err = result.Result()\n\t\tif !finished {\n\t\t\treturn nil, errors.New(\"initializing the main module hasn't finished, this is a bug in k6 please report it\")\n\t\t}\n\t}\n\n\tif err != nil {\n\t\tif exception, ok := errors.AsType[*sobek.Exception](err); ok {\n\t\t\terr = &scriptExceptionError{inner: exception}\n\t\t}\n\t\treturn nil, err\n\t}\n\n\t// If we've already initialized the original VU init context, forbid\n\t// any subsequent VUs to open new files\n\tif vuID == 0 {\n\t\tallowOnlyOpenedFiles(b.filesystems[\"file\"])\n\t}\n\n\trt.SetRandSource(common.NewRandSource())\n","sourceCodeStart":357,"sourceCodeEnd":393,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/bundle.go#L357-L393","documentation":"An internal invariant in the bundle init path (internal/js/bundle.go:372): after the VU event loop finishes running the main module's top-level code, result.Result() must report completion. If the module future is not finished, k6 returns this explicit 'this is a bug in k6 please report it' error rather than continuing in an undefined state. It typically surfaces with unusual top-level await / async module-init patterns.","triggerScenarios":"Initializing a script whose main module uses top-level await, async imports, or timing patterns that leave the module-init promise unresolved when the event loop drains; often a regression in a specific k6 version rather than a user config error.","commonSituations":"Upgrading k6 and having previously-working scripts with top-level await hit the invariant; scripts mixing module-level async initialization with imported ESM bundles.","solutions":["Upgrade to the latest k6 release — this class of bug gets fixed upstream.","Report the issue at github.com/grafana/k6/issues with the script and k6 version.","As a workaround, move top-level await into an async setup() function or wrap initialization in an explicitly awaited call."],"exampleFix":"// before (module top-level)\nconst data = await fetchData();\nexport default function () { ... }\n// after\nexport async function setup() { return await fetchData(); }\nexport default function (data) { ... }","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pin the k6 version in CI and test candidate upgrades on a canary script with top-level await.","Prefer setup() for async initialization instead of top-level await in the main module.","When you hit it, capture k6 version + minimal reproducer and check/report upstream before debugging your script."],"tags":["internal","bug","module-init","eventloop","async"],"backgroundTag":"internal-invariant-violation","analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}