grafana/k6 · error · errRunInInitContext
getting scenario information outside of the VU context is…
Error message
getting scenario information outside of the VU context is not supported
What it means
The execution module exposes live test metadata through property accessors such as exec.scenario and exec.vu. newScenarioInfo creates the scenario object lazily, and scenario data only exists once a VU is actually running inside a scenario - so the accessor returns errRunInInitContext when vu.State() is nil (init context).
Solutions
- Move the exec.scenario read inside export default function() (or setup/teardown where a VU state exists) and branch per-iteration instead of once at init.
- If you need static per-scenario configuration, use the options.scenarios object itself (imported via exec.test.options or the options export), which is available in init.
- Compute lazily: read exec.scenario.name inside the first iteration and cache it in a module-level variable.
Example fix
// before
import exec from 'k6/execution';
const scenarioName = exec.scenario.name; // throws in init context
// after
import exec from 'k6/execution';
export default function () {
const scenarioName = exec.scenario.name; // ok, runs per VU iteration
} Defensive patterns
Strategy: validation
Validate before calling
// exec.scenario is only valid inside a VU - never read it at module scope.
// Safe pattern: read lazily inside the default function.
export default function () {
const scenario = exec.scenario; // guaranteed VU context here
if (scenario.name === 'breakpoint') { /* ... */ }
} Try / catch
try {
const name = exec.scenario.name;
} catch (e) {
if (/outside of the VU context/.test(e.message)) {
console.warn('exec.scenario read outside VU context - returning fallback');
return 'unknown';
}
throw e;
} Prevention
- Treat exec.scenario/exec.vu as per-iteration data, like http response codes.
- For init-time branching use options.scenarios or exec.test.options instead.
When it happens
Trigger: Reading exec.scenario (any property, e.g. exec.scenario.name) at the top level of the script or in any code that runs before VU iteration starts, where the VU state is not yet attached. The error surfaces as the property-get rejection.
Common situations: Trying to branch scenario options at init time (e.g. picking tags per scenario before the test starts); building a global config object at module scope that reads exec.scenario; logging exec.scenario for debugging at import time.
Related errors
- 110
- csv Parser constructor must be called in the init context
- load must be called in the init context
- new SharedArray must be called in the init context
- 104
AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18).
Data as JSON: /api/errors/5ead4fb8e90be5cd.
Report an issue: GitHub.
Appendix: source
Thrown at internal/js/modules/k6/execution/execution.go:74
common.Throw(rt, err)
}
}
defProp("instance", mi.newInstanceInfo)
defProp("scenario", mi.newScenarioInfo)
defProp("test", mi.newTestInfo)
defProp("vu", mi.newVUInfo)
mi.obj = o
return mi
}
// Exports returns the exports of the execution module.
func (mi *ModuleInstance) Exports() modules.Exports {
return modules.Exports{Default: mi.obj}
}
var errRunInInitContext = errors.New("getting scenario information outside of the VU context is not supported")
// newScenarioInfo returns a sobek.Object with property accessors to retrieve
// information about the scenario the current VU is running in.
func (mi *ModuleInstance) newScenarioInfo() (*sobek.Object, error) {
rt := mi.vu.Runtime()
vuState := mi.vu.State()
if vuState == nil {
return nil, errRunInInitContext
}
getScenarioState := func() *lib.ScenarioState {
ss := lib.GetScenarioState(mi.vu.Context())
if ss == nil {
common.Throw(rt, errRunInInitContext)
}
return ss
}
si := map[string]func() any{View on GitHub (pinned to 01ffac6f24)