{"id":"ef618324cd1249cc","repo":"go-redis/redis","slug":"connection-in-unexpected-state-after-initializatio","errorCode":null,"errorMessage":"connection in unexpected state after initialization: %s","messagePattern":"connection in unexpected state after initialization: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"redis.go","lineNumber":708,"sourceCode":"\t\t\t\t}\n\t\t\t\tif cancel != nil {\n\t\t\t\t\tdefer cancel()\n\t\t\t\t}\n\n\t\t\t\tfinalState, err := cn.GetStateMachine().AwaitAndTransition(\n\t\t\t\t\twaitCtx,\n\t\t\t\t\t[]pool.ConnState{pool.StateIdle, pool.StateInUse},\n\t\t\t\t\tpool.StateIdle, // Target is IDLE (but we're already there, so this is a no-op)\n\t\t\t\t)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t\t// Verify we're now initialized\n\t\t\t\tif finalState == pool.StateIdle || finalState == pool.StateInUse {\n\t\t\t\t\treturn nil\n\t\t\t\t}\n\t\t\t\t// Unexpected state after waiting\n\t\t\t\treturn fmt.Errorf(\"connection in unexpected state after initialization: %s\", finalState)\n\t\t\t}\n\n\t\t\t// Unexpected state (CLOSED, UNUSABLE, etc.)\n\t\t\treturn err\n\t\t}\n\t}\n\n\t// At this point, we're in INITIALIZING state and we own the initialization\n\t// If we fail, we must transition to CLOSED\n\tvar initErr error\n\tconnPool := pool.NewSingleConnPool(c.connPool, cn)\n\t// The handshake Conn (handed to OnConnect) must share the client's\n\t// HIMPORT registry: a private registry restarts versions at 1, so an\n\t// OnConnect prepare would mark the pooled connection with a version\n\t// number that collides with the client registry's and silently skips\n\t// the replay of a different fieldset definition.\n\tconn := newConn(c.opt, connPool, &c.hooksMixin, c.himport)\n\t// The internal wrapper does not serve cached reads, but it needs the","sourceCodeStart":690,"sourceCodeEnd":726,"githubUrl":"https://github.com/go-redis/redis/blob/36d97525cd8076aed67cddf54778e9ea84550929/redis.go#L690-L726","documentation":"Error \"connection in unexpected state after initialization: %s\" thrown in go-redis/redis.","triggerScenarios":"Thrown at redis.go:708 when the library encounters an invalid state.","commonSituations":"Keep OnConnect hooks minimal and error-propagating so init failures are explicit.","solutions":["Recreate the client; a connection ended initialization in the state shown in the error","Check OnConnect hooks and credential providers for errors leaving conns in odd states"],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"analyzedSha":"36d97525cd8076aed67cddf54778e9ea84550929","analyzedAt":"2026-08-06T01:08:27.376Z","schemaVersion":2}