grpc/grpc-go · error · errorNilServer

profiling: no grpc.Server provided

Error message

profiling: no grpc.Server provided

What it means

Returned by profiling.Init (profiling/service/service.go:61, used at 67-69) when ProfilingConfig.Server is nil. The profiling package must register its ProfilingServer service on a *grpc.Server, so a nil server makes initialization meaningless and is rejected.

Source

Thrown at profiling/service/service.go:61

// ProfilingConfig defines configuration options for the Init method.
type ProfilingConfig struct {
	// Setting this to true will enable profiling.
	Enabled bool

	// Profiling uses a circular buffer (ring buffer) to store statistics for
	// only the last few RPCs so that profiling stats do not grow unbounded. This
	// parameter defines the upper limit on the number of RPCs for which
	// statistics should be stored at any given time. An average RPC requires
	// approximately 2-3 KiB of memory for profiling-related statistics, so
	// choose an appropriate number based on the amount of memory you can afford.
	StreamStatsSize uint32

	// To expose the profiling service and its methods, a *grpc.Server must be
	// provided.
	Server *grpc.Server
}

var errorNilServer = errors.New("profiling: no grpc.Server provided")

// Init takes a *ProfilingConfig to initialize profiling (turned on/off
// depending on the value set in pc.Enabled) and register the profiling service
// in the server provided in pc.Server.
func Init(pc *ProfilingConfig) error {
	if pc.Server == nil {
		return errorNilServer
	}

	if err := profiling.InitStats(pc.StreamStatsSize); err != nil {
		return err
	}

	ppb.RegisterProfilingServer(pc.Server, getProfilingServerInstance())

	// Do this last after everything has been initialized and allocated.
	profiling.Enable(pc.Enabled)

View on GitHub (pinned to 03255a9237)

Solutions

  1. Set ProfilingConfig.Server to your *grpc.Server before calling profiling.Init.
  2. Ensure grpc.NewServer() runs and returns a non-nil server prior to profiling setup.
  3. Skip profiling.Init entirely if you have no server (e.g. client-only process).
  4. Guard with: if pc.Server == nil { return errors.New(...) } with a clearer message.

Example fix

// before
profiling.Init(&profiling.ProfilingConfig{
    Enabled: true, StreamStatsSize: 1000, // Server nil
})

// after
srv := grpc.NewServer()
profiling.Init(&profiling.ProfilingConfig{
    Enabled: true, StreamStatsSize: 1000, Server: srv,
})
Defensive patterns

Strategy: validation

Validate before calling

if pc.Server == nil {
    return errors.New("profiling requires a non-nil grpc.Server")
}

Try / catch

if err := profiling.Init(pc); err != nil {
    if errors.Is(err, profiling.ErrorNilServer) /* or string match */ {
        return fmt.Errorf("create the server first: %w", err)
    }
}

Prevention

When it happens

Trigger: Calling profiling.Init(&ProfilingConfig{Enabled: true, StreamStatsSize: 100}) without setting the Server field; passing a config built before the server is constructed.

Common situations: Building the ProfilingConfig before grpc.NewServer and forgetting to assign it; wiring profiling in a test without a real server; conditional profiling code path that skips server creation.

Related errors


AI-assisted analysis of grpc/grpc-go@03255a9237 (2026-08-07). Data as JSON: /api/errors/84374e432af8a7fe. Report an issue: GitHub.