Skip to content

go panic when using group() and try/finallyΒ #5713

Closed

Description

@amontfaucon

Brief summary

In a script, I started to get runtime panic() when adding a try/finally block. I reduced the issue to its simplest form, it appears that a specific mix of group(), variables allocated globally and try/finally causes sobek to crash.

I attached several slightly altered versions of the crashing one that run fine.

This use case was important to me as finalizer was actually a Counter instance to track successes and failures. However, it's not the root cause.

k6 version

k6 v1.6.1 (commit/2ac2bb560e, go1.25.7, linux/amd64)

OS

Linux

Docker version and image (if applicable)

No response

Steps to reproduce the problem

Run this script: ./k6 run crash.js

import { group } from "k6";

const finalizer = () => {
};

export default function () {
    try {
        const work = () => {
        };

        group("Home page", function () {
            work();
        });

        if (2 > Math.random()) {
            return;
        }
    } finally {
        finalizer();
    }
};

Expected behaviour

No panic

Actual behaviour

panics with k6 1.6.1:

panic: runtime error: index out of range [2] with length 1 [recovered, repanicked]

goroutine 81 [running]:
github.com/grafana/sobek.(*Runtime).runWrapped.func1()
	github.com/grafana/sobek@v0.0.0-20260121195222-d8d9202018c5/runtime.go:2521 +0xe5
panic({0x1dbbf40?, 0xc000046b28?})
	runtime/panic.go:783 +0x132
github.com/grafana/sobek.(*vm).handleThrow(0xc000412360, {0x1dbbf40, 0xc000046b28})
	github.com/grafana/sobek@v0.0.0-20260121195222-d8d9202018c5/vm.go:847 +0x425
github.com/grafana/sobek.(*vm).try.func1()
	github.com/grafana/sobek@v0.0.0-20260121195222-d8d9202018c5/vm.go:866 +0x3f
panic({0x1dbbf40?, 0xc000046b28?})
	runtime/panic.go:783 +0x132
github.com/grafana/sobek.(*vm).handleThrow(0xc000412360, {0x1dbbf40, 0xc000046b28})
	github.com/grafana/sobek@v0.0.0-20260121195222-d8d9202018c5/vm.go:847 +0x425
github.com/grafana/sobek.(*vm).runTryInner.func1()
	github.com/grafana/sobek@v0.0.0-20260121195222-d8d9202018c5/vm.go:889 +0x3f
panic({0x1dbbf40?, 0xc000046b28?})
	runtime/panic.go:783 +0x132
github.com/grafana/sobek.(*stash).getByIdx(...)
	github.com/grafana/sobek@v0.0.0-20260121195222-d8d9202018c5/vm.go:496
github.com/grafana/sobek.loadStashLex.exec(0xf7fe25?, 0x6?)
	github.com/grafana/sobek@v0.0.0-20260121195222-d8d9202018c5/vm.go:3145 +0x1db
github.com/grafana/sobek.(*vm).run(0xc000412360)
	github.com/grafana/sobek@v0.0.0-20260121195222-d8d9202018c5/vm.go:642 +0x5b
github.com/grafana/sobek.(*vm).runTryInner(0xc000412360?)
	github.com/grafana/sobek@v0.0.0-20260121195222-d8d9202018c5/vm.go:893 +0x52
github.com/grafana/sobek.(*baseJsFuncObject).__call(0xc0005fe240, {0xc0000cd700?, 0x1, 0x1dbcf00?}, {0x0, 0x0}, {0x228a848?, 0x33a6a00?})
	github.com/grafana/sobek@v0.0.0-20260121195222-d8d9202018c5/func.go:434 +0x711
github.com/grafana/sobek.(*baseJsFuncObject)._call(...)
	github.com/grafana/sobek@v0.0.0-20260121195222-d8d9202018c5/func.go:450
github.com/grafana/sobek.(*baseJsFuncObject).call(0x1dbcf00?, {{0x228a848, 0x33a6a00}, {0xc0000cd700, 0x1, 0x1}}, {0x0?, 0x0?})
	github.com/grafana/sobek@v0.0.0-20260121195222-d8d9202018c5/func.go:458 +0x6d
github.com/grafana/sobek.(*baseJsFuncObject).Call(...)
	github.com/grafana/sobek@v0.0.0-20260121195222-d8d9202018c5/func.go:390
github.com/grafana/sobek.AssertFunction.func1.1()
	github.com/grafana/sobek@v0.0.0-20260121195222-d8d9202018c5/runtime.go:2481 +0x56
github.com/grafana/sobek.(*vm).try(0xc000412360, 0xc000487908)
	github.com/grafana/sobek@v0.0.0-20260121195222-d8d9202018c5/vm.go:870 +0x234
github.com/grafana/sobek.(*Runtime).runWrapped(0xc00033a408, 0x47e685?)
	github.com/grafana/sobek@v0.0.0-20260121195222-d8d9202018c5/runtime.go:2525 +0x5f
github.com/grafana/sobek.AssertFunction.func1({0x228a848?, 0x33a6a00?}, {0xc0000cd700?, 0x1?, 0x1?})
	github.com/grafana/sobek@v0.0.0-20260121195222-d8d9202018c5/runtime.go:2480 +0x8c
go.k6.io/k6/internal/js.(*VU).runFn.func2()
	go.k6.io/k6/internal/js/runner.go:942 +0x3e
go.k6.io/k6/internal/js/eventloop.(*EventLoop).Start(0xc0002d23c0, 0xc0004ac540)
	go.k6.io/k6/internal/js/eventloop/eventloop.go:179 +0x197
go.k6.io/k6/internal/js.(*VU).runFn(0xc0005903c0, {0x2272178, 0xc000350be0}, 0x1, 0xc0004d7c98, 0xc0000cd6e0, {0xc0000cd700, 0x1, 0x1})
	go.k6.io/k6/internal/js/runner.go:941 +0x270
go.k6.io/k6/internal/js.(*ActiveVU).RunOnce(0xc0003d6cc0)
	go.k6.io/k6/internal/js/runner.go:874 +0x487
go.k6.io/k6/lib/executor.PerVUIterations.Run.getIterationRunner.func7({0x2272140, 0xc00058f080}, {0x225fd20?, 0xc0003d6cc0?})
	go.k6.io/k6/lib/executor/helpers.go:81 +0x4a
go.k6.io/k6/lib/executor.PerVUIterations.Run.func5({0x2269620, 0xc0005903c0})
	go.k6.io/k6/lib/executor/per_vu_iterations.go:228 +0x39a
created by go.k6.io/k6/lib/executor.PerVUIterations.Run in goroutine 7
	go.k6.io/k6/lib/executor/per_vu_iterations.go:241 +0xb85

Crashing script as well as several very close versions that do work fine:

crash.js
work01.js
work02.js
work03.js
work04.js
work05.js

Activity

  1. self-assigned this
    on Mar 6, 2026
  2. mstoykov commented on Mar 6, 2026

    @mstoykov
    Contributor

    Hi @amontfaucon , thank you for reporting this. I have been able to reproduce it and unfortunately some recent updates upstream are not fixing it.

    I have also confirmed it started in k6 v0.40.0 (at least for the main script) and tha v0.39.0 and bunch of previous versions do not have the problem.

    I will try to work on this next week so it hopefulyl will land in the next release.

  3. taras commented on Mar 9, 2026

    @taras

    @mstoykov I created PR 122, I hope it's useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions