grafana/k6 · error

initializing the main module hasn't finished, this is a bug

Error message

initializing the main module hasn't finished, this is a bug in k6 please report it

What it means

An internal invariant guard during bundle instantiation: after the VU event loop finishes, result.Result() reports the main module's load as not finished. k6 classifies this as an engine bug and asks the user to report it — it is not a script-authoring error.

Source

Thrown at internal/js/bundle.go:372

		var err error
		result, err = modSys.RunSourceData(b.sourceData)
		return err
	}

	call, _ := sobek.AssertFunction(vuImpl.runtime.ToValue(callback))

	err = vuImpl.eventLoop.Start(func() error {
		_, err := call(nil)
		return err
	})

	<-initDone

	if err == nil {
		var finished bool
		bi.mainModule, finished, err = result.Result()
		if !finished {
			return nil, errors.New("initializing the main module hasn't finished, this is a bug in k6 please report it")
		}
	}

	if err != nil {
		var exception *sobek.Exception
		if errors.As(err, &exception) {
			err = &scriptExceptionError{inner: exception}
		}
		return nil, err
	}

	// If we've already initialized the original VU init context, forbid
	// any subsequent VUs to open new files
	if vuID == 0 {
		allowOnlyOpenedFiles(b.filesystems["file"])
	}

	rt.SetRandSource(common.NewRandSource())

View on GitHub (pinned to 93accf6570)

Solutions

  1. Re-run the test — transient races usually do not reproduce
  2. If reproducible, open an issue at https://github.com/grafana/k6/issues with the script, `k6 version`, and verbose logs (`--log-output=stdout`)
  3. Bisect the script's imports to identify which module triggers it and include that in the report
Defensive patterns

Strategy: retry

Try / catch

# Rare internal race: retry once, then report
for i in 1 2; do k6 run script.js && break || [ $i -eq 2 ] && { echo 'Persistent init bug — report at github.com/grafana/k6/issues with k6 version + logs' >&2; exit 1; }; done

Prevention

When it happens

Trigger: A race in asynchronous module initialization where the module-load promise has not settled when the event loop drains; ordinary script content does not produce it.

Common situations: Almost never seen; can surface with remote/E SM module loading or custom native (Go) extensions that yield during init on heavily loaded machines.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/62d03583a18e7dc4. Report an issue: GitHub.