Blocking API call rejected in k6 init context
0 reputation · 19 Nov 2023, 20:33 UTC
0 reputation · 19 Nov 2023, 20:33 UTC
Determine whether a k6 initialization routine can call a blocking function such as sleep in the init context, or whether the call must be moved to the default VU function.
k6 executes init code once at startup and default code per virtual-user iteration. Blocking APIs are rejected in init to prevent startup stalls, but the exact list of disallowed functions and enforcement behavior can change between k6 releases. The installed version should be treated as a version assumption until checked against release notes.
34775 reputation · 19 Nov 2023, 23:04 UTC
No. A blocking call such as sleep() cannot run in k6 init context. Move it to the default exported function or another per-VU function. Init runs once at startup before any virtual user exists, and k6 rejects blocking APIs there at runtime to prevent startup stalls.
sleep() and exec() from k6/exec in init with the runtime error 'blocking API call rejected in init context'.k6 run script.js, not during syntax checking.Any call that blocks the VU event loop is prohibited in init because it would delay startup for every VU. Enforcement is strongest for sleep and exec; other blocking I/O may be allowed in some releases but is discouraged. Treat version-specific lists as assumptions until you check the release notes for your installed k6 version.
No. Moving a blocking call from init to default changes the timing from once per test to once per VU per iteration. If you need a one-time startup delay, init cannot provide it with a blocking API. Use non-blocking setup in init and place per-VU delays in the default function.
k6 version. Compare it against the k6 release notes before relying on any version-specific exception.export default function.import { sleep } from 'k6';
export const options = { vus: 1, iterations: 1 };
export default function () {
sleep(1); // allowed: pauses this VU iteration
}
k6 run script.js and confirm the init error does not appear and the pause happens per iteration.One missing diagnostic detail that changes the recommendation: do you need the delay once per test or once per VU, and which executor are you using? Shared-iterations and per-VU-iteration executors behave differently, so the answer determines whether a default-function delay is acceptable or whether you need a different pattern.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.