{"record":{"id":"82330619aaa6fd92","repo":"clockworklabs/SpacetimeDB","slug":"received-initialconnection-with-unexpected-identit","errorCode":null,"errorMessage":"Received InitialConnection with unexpected identity. Previous={identity}, New={initialConnection.Identity}","messagePattern":"Received InitialConnection with unexpected identity\\. Previous=(.+?), New=(.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sdks/csharp/src/SpacetimeDBClient.cs","lineNumber":813,"sourceCode":"                        {\n                            var legacyEventContext = ToEventContext(new Event<Reducer>.Reducer(reducerEvent));\n                            ApplyUpdate(legacyEventContext, dbOps);\n                            var eventContext = ToReducerEventContext(reducerEvent);\n                            Dispatch(eventContext, reducerEvent.Reducer);\n                        }\n                        else\n                        {\n                            var legacyEventContext = ToEventContext(new Event<Reducer>.UnknownTransaction());\n                            ApplyUpdate(legacyEventContext, dbOps);\n                        }\n                        break;\n                    }\n                case ServerMessage.InitialConnection(var initialConnection):\n                    try\n                    {\n                        if (Identity is Identity identity && identity != initialConnection.Identity)\n                        {\n                            throw new InvalidOperationException(\n                                $\"Received InitialConnection with unexpected identity. Previous={identity}, New={initialConnection.Identity}\"\n                            );\n                        }\n\n                        if (initialConnectionId is ConnectionId connectionId\n                            && connectionId != initialConnection.ConnectionId)\n                        {\n                            throw new InvalidOperationException(\n                                $\"Received InitialConnection with unexpected connection_id. Previous={connectionId}, New={initialConnection.ConnectionId}\"\n                            );\n                        }\n\n                        Identity = initialConnection.Identity;\n                        initialConnectionId = initialConnection.ConnectionId;\n                        if (!onConnectInvoked)\n                        {\n                            onConnectInvoked = true;\n                            onConnect?.Invoke(initialConnection.Identity, initialConnection.Token);","sourceCodeStart":795,"sourceCodeEnd":831,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/524b4487d949b61a07d4f39c862d1290259dfd20/sdks/csharp/src/SpacetimeDBClient.cs#L795-L831","documentation":"When InitialConnection arrives, the client compares the server-assigned identity against the stored one and throws on mismatch. The surrounding handler catches the exception and only logs it (Log.Exception) — onConnect is never invoked and the stale identity remains, so the app typically hangs 'connecting'. Since Connect() resets Identity to null, this fires on a second, differing InitialConnection within one connection lifetime.","triggerScenarios":"A duplicated/replayed InitialConnection with a different identity: token switched mid-connection, a proxy or load balancer replaying websocket frames, or test code injecting multiple InitialConnection messages.","commonSituations":"Auth-token refresh implemented by reconnecting on the same live connection; misbehaving intermediaries duplicating frames; integration tests feeding canned message sequences.","solutions":["When the auth token changes, Disconnect and build a new DbConnection instead of reusing the live one","Check client logs for the Log.Exception entry — an app stuck after connect usually points here","Verify proxies/LBs in front of the server are not replaying websocket traffic"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// If you must change tokens, verify and rebuild rather than reusing the session:\nif (tokenChanged)\n{\n    conn.Disconnect();\n    conn = NewBuilder().WithUri(uri).WithDatabaseName(db).WithToken(newToken).Build();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use one DbConnection per (token, database) session; rebuild on token change","Monitor for 'connected but OnConnect never fired' — that is this error's signature in the logs","Ensure intermediaries do not replay websocket frames"],"tags":["identity","reconnect","connection-lifecycle","csharp"],"backgroundTag":"identity-mismatch-on-reconnect","analyzedSha":"524b4487d949b61a07d4f39c862d1290259dfd20","analyzedAt":"2026-08-16T23:58:54.611Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}