go-redis/redis · error

cannot initialize connection from state %s: %w

Error message

cannot initialize connection from state %s: %w

What it means

Error "cannot initialize connection from state %s: %w" thrown in go-redis/redis.

Source

Thrown at internal/pool/conn.go:815

	// Wait for and transition to INITIALIZING state - this prevents concurrent initializations
	// Valid from states: CREATED (first init), IDLE (reconnect), UNUSABLE (handoff/reauth)
	// If another goroutine is initializing, we'll wait for it to finish
	// if the context has a deadline, use that, otherwise use the connection read (relaxed) timeout
	// which should be set during handoff. If it is not set, use a 5 second default
	deadline, ok := ctx.Deadline()
	if !ok {
		deadline = time.Now().Add(cn.getEffectiveReadTimeout(5 * time.Second))
	}
	waitCtx, cancel := context.WithDeadline(ctx, deadline)
	defer cancel()
	// Use predefined slice to avoid allocation
	finalState, err := cn.stateMachine.AwaitAndTransition(
		waitCtx,
		validFromCreatedIdleOrUnusable,
		StateInitializing,
	)
	if err != nil {
		return fmt.Errorf("cannot initialize connection from state %s: %w", finalState, err)
	}

	if cn.onCscReinit != nil {
		cn.onCscReinit()
	}

	// Replace the underlying connection
	cn.SetNetConn(netConn)

	// Execute initialization
	// NOTE: ExecuteInitConn (via baseClient.initConn) will transition to IDLE on success
	// or CLOSED on failure. We don't need to do it here.
	// NOTE: Initconn returns conn in IDLE state
	initErr := cn.ExecuteInitConn(ctx)
	if initErr != nil {
		// ExecuteInitConn already transitioned to CLOSED, just return the error
		return initErr
	}

View on GitHub (pinned to 36d97525cd)

Solutions

  1. Do not reuse a connection object across lifecycle states; obtain a fresh conn from the pool
  2. Investigate the wrapped error to see why the connection entered an unexpected state

When it happens

Trigger: Thrown at internal/pool/conn.go:815 when the library encounters an invalid state.

Common situations: Treat connections as single-lifecycle resources managed exclusively by the pool.


AI-assisted analysis of go-redis/redis@36d97525cd (2026-08-06). Data as JSON: /data/errors/8f730adc5ad062cd.json. Report an issue: GitHub.