Skip to content

k6/websockets panics on new WebSocket(...) in the init context #6353

Description

@mem

Brief summary

Unlike other network-capable modules (k6/http, k6/ws, k6/grpc), k6/websockets has no guard against being used in the init context (i.e. before any VU state exists). Calling new WebSocket(...) at the top level of a script's module body causes a nil-pointer-dereference panic instead of an error, crashing k6 inspect and k6 run with exit code 109. The deprecated alias k6/experimental/websockets is equally affected, since it wraps the same implementation.

k6 version

v2.2.0

OS

linux

Docker version and image (if applicable)

N/A

Steps to reproduce the problem

import { WebSocket } from 'k6/websockets';

new WebSocket('ws://example.invalid');

export default function () {}
$ k6 run websockets.js
time="2026-08-28T13:12:18-06:00" level=error msg="unexpected k6 panic: runtime error: invalid memory address or nil pointer dereference\ngoroutine 1 [running]:...

Expected behaviour

k6 doesn't panic

Actual behaviour

k6 panics

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions