{"id":"8f730adc5ad062cd","repo":"go-redis/redis","slug":"cannot-initialize-connection-from-state-s-w","errorCode":null,"errorMessage":"cannot initialize connection from state %s: %w","messagePattern":"cannot initialize connection from state (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/pool/conn.go","lineNumber":815,"sourceCode":"\t// Wait for and transition to INITIALIZING state - this prevents concurrent initializations\n\t// Valid from states: CREATED (first init), IDLE (reconnect), UNUSABLE (handoff/reauth)\n\t// If another goroutine is initializing, we'll wait for it to finish\n\t// if the context has a deadline, use that, otherwise use the connection read (relaxed) timeout\n\t// which should be set during handoff. If it is not set, use a 5 second default\n\tdeadline, ok := ctx.Deadline()\n\tif !ok {\n\t\tdeadline = time.Now().Add(cn.getEffectiveReadTimeout(5 * time.Second))\n\t}\n\twaitCtx, cancel := context.WithDeadline(ctx, deadline)\n\tdefer cancel()\n\t// Use predefined slice to avoid allocation\n\tfinalState, err := cn.stateMachine.AwaitAndTransition(\n\t\twaitCtx,\n\t\tvalidFromCreatedIdleOrUnusable,\n\t\tStateInitializing,\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"cannot initialize connection from state %s: %w\", finalState, err)\n\t}\n\n\tif cn.onCscReinit != nil {\n\t\tcn.onCscReinit()\n\t}\n\n\t// Replace the underlying connection\n\tcn.SetNetConn(netConn)\n\n\t// Execute initialization\n\t// NOTE: ExecuteInitConn (via baseClient.initConn) will transition to IDLE on success\n\t// or CLOSED on failure. We don't need to do it here.\n\t// NOTE: Initconn returns conn in IDLE state\n\tinitErr := cn.ExecuteInitConn(ctx)\n\tif initErr != nil {\n\t\t// ExecuteInitConn already transitioned to CLOSED, just return the error\n\t\treturn initErr\n\t}","sourceCodeStart":797,"sourceCodeEnd":833,"githubUrl":"https://github.com/go-redis/redis/blob/36d97525cd8076aed67cddf54778e9ea84550929/internal/pool/conn.go#L797-L833","documentation":"Error \"cannot initialize connection from state %s: %w\" thrown in go-redis/redis.","triggerScenarios":"Thrown at internal/pool/conn.go:815 when the library encounters an invalid state.","commonSituations":"Treat connections as single-lifecycle resources managed exclusively by the pool.","solutions":["Do not reuse a connection object across lifecycle states; obtain a fresh conn from the pool","Investigate the wrapped error to see why the connection entered an unexpected state"],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"analyzedSha":"36d97525cd8076aed67cddf54778e9ea84550929","analyzedAt":"2026-08-06T01:08:27.376Z","schemaVersion":2}