{"record":{"id":"c377a312c1c28033","repo":"grafana/k6","slug":"test-run-completed-before-k6-could-start","errorCode":null,"errorMessage":"test run completed before k6 could start","messagePattern":"test run completed before k6 could start","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cloudapi/provisioning/api.go","lineNumber":167,"sourceCode":"\t\tswitch v6.Status(status) {\n\t\tcase v6.StatusInitializing:\n\t\t\treturn nil\n\n\t\tcase v6.StatusAborted:\n\t\t\tvar abortMsg string\n\t\t\tfor _, e := range progress.StatusHistory {\n\t\t\t\tif e.Status == v6.StatusAborted && e.Message != \"\" {\n\t\t\t\t\tabortMsg = e.Message\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t}\n\t\t\tif abortMsg != \"\" {\n\t\t\t\treturn fmt.Errorf(\"test run aborted before starting: %s\", abortMsg)\n\t\t\t}\n\t\t\treturn fmt.Errorf(\"test run aborted before starting\")\n\n\t\tcase v6.StatusCompleted:\n\t\t\treturn fmt.Errorf(\"test run completed before k6 could start\")\n\n\t\tdefault:\n\t\t\t// created, queued, or any unrecognised state — keep polling.\n\t\t\tif status != lastStatus {\n\t\t\t\tc.logger.WithField(\"status\", progress.FormatStatus()).Debug(\"test status\")\n\t\t\t\tlastStatus = status\n\t\t\t}\n\t\t}\n\n\t\tselect {\n\t\tcase <-ctx.Done():\n\t\t\treturn ctx.Err()\n\t\tcase <-time.After(pollInterval):\n\t\t\t// continue polling\n\t\t}\n\t}\n}\n","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/cloudapi/provisioning/api.go#L149-L185","documentation":"Returned by Client.WaitForTestRunReady (internal/cloudapi/provisioning/api.go:167) when polling GET /cloud/v6/test_runs/{id} during --local-execution provisioning. The loop waits for status 'initializing' as the signal that k6 may begin local execution; if the backend reports 'completed' first, this k6 process can never attach to the run and the error is returned immediately.","triggerScenarios":"ProvisionLocalExecution -> WaitForTestRunReady observes v6.StatusCompleted before v6.StatusInitializing. Happens when another executor already finished the run, when the same test run ID is re-used by a parallel process, or when a backend state race skips 'initializing' entirely.","commonSituations":"Duplicate/parallel k6 processes provisioning the same load test with the same token; retrying an old provisioning request that references a stale test run; backend-side automation that completed or force-completed the run between creation and the first poll.","solutions":["Check the Grafana Cloud UI for the run's status history to see who/what completed it","Ensure only one k6 process provisions this load test at a time","Start a fresh test run instead of reusing the previous testRunID","If it recurs consistently, capture the status transitions via debug logging (k6 sets 'test status' Debug lines) and report a backend issue"],"exampleFix":"// before\nif err := client.WaitForTestRunReady(ctx, testRunID, 0); err != nil {\n\treturn err\n}\n\n// after\nif err := client.WaitForTestRunReady(ctx, testRunID, 0); err != nil {\n\tif strings.Contains(err.Error(), \"completed before k6 could start\") {\n\t\t// run already finished server-side; provision a new run\n\t\treturn provisionFreshRun(ctx)\n\t}\n\treturn err\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"func isCompletedBeforeStart(err error) bool {\n\treturn err != nil && strings.Contains(err.Error(), \"test run completed before k6 could start\")\n}","tryCatchPattern":"err := client.WaitForTestRunReady(ctx, resp.TestRunID, pollInterval)\nif err != nil {\n\tif isCompletedBeforeStart(err) {\n\t\t// terminal: provision a fresh run rather than retrying this one\n\t}\n\treturn err\n}","preventionTips":["Provision one run per k6 process; never share a testRunID across processes","Check the run's status in the Grafana Cloud UI before retrying provisioning","Log status transitions (the client already emits Debug lines) to catch state races early"],"tags":["cloud-api","provisioning","polling","state-machine","local-execution"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}