grafana/k6 · error

no gRPC connection, you must call connect first

Error message

no gRPC connection, you must call connect first

What it means

Both client.invoke() and client.asyncInvoke() build the request via buildInvokeRequest, which requires an established connection (c.conn). c.conn is only set by client.connect() and cleared by client.close(), so invoking without a preceding successful connect throws this error.

Source

Thrown at internal/js/modules/k6/grpc/client.go:356

	}()

	return promise, nil
}

// buildInvokeRequest creates a new InvokeRequest from the given method name, request object and parameters
func (c *Client) buildInvokeRequest(
	method string,
	req sobek.Value,
	params sobek.Value,
) (grpcext.InvokeRequest, error) {
	grpcReq := grpcext.InvokeRequest{}

	state := c.vu.State()
	if state == nil {
		return grpcReq, common.NewInitContextError("invoking RPC methods in the init context is not supported")
	}
	if c.conn == nil {
		return grpcReq, errors.New("no gRPC connection, you must call connect first")
	}
	if method == "" {
		return grpcReq, errors.New("method to invoke cannot be empty")
	}
	if method[0] != '/' {
		method = "/" + method
	}
	methodDesc := c.mds[method]
	if methodDesc == nil {
		return grpcReq, fmt.Errorf("method %q not found in file descriptors", method)
	}

	p, err := newCallParams(c.vu, params)
	if err != nil {
		return grpcReq, fmt.Errorf("invalid GRPC's client.invoke() parameters: %w", err)
	}

	// k6 GRPC Invoke's default timeout is 2 minutes

View on GitHub (pinned to 93accf6570)

Solutions

  1. Call client.connect(addr) in the init context (or at the start of the default function) before any invoke
  2. Ensure client.close() runs only after all invokes (end of default function or teardown)
  3. Check that no early return, exception, or branch skips the connect call on any code path

Example fix

// before
export default function () {
  client.invoke('/hello.HelloService/SayHello', {});
}

// after
client.connect('grpcbin.example.com:443');
export default function () {
  client.invoke('/hello.HelloService/SayHello', {});
}
Defensive patterns

Strategy: validation

Validate before calling

const client = new grpc.Client();
client.connect(addr);
// or, guarding a shared helper:
function ensureConnected(client, addr) {
  if (!client || !isConnected) { client.connect(addr); isConnected = true; }
}

Prevention

When it happens

Trigger: Calling client.invoke(...) before client.connect(...); calling invoke after client.close() (e.g. close moved into a shared helper or teardown ordering bug); connect() placed inside an if-branch or loop iteration that did not execute.

Common situations: Refactoring connect() into conditional initialization; closing the client in one scenario while another scenario or handleSummary-like code still invokes; copy-paste between scripts where connect was dropped.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/a5afdb5d227bcd33. Report an issue: GitHub.