{"record":{"id":"67d176782a33e224","repo":"clockworklabs/SpacetimeDB","slug":"received-initialconnection-with-unexpected-connect","errorCode":null,"errorMessage":"Received InitialConnection with unexpected connection_id. Previous={connectionId}, New={initialConnection.ConnectionId}","messagePattern":"Received InitialConnection with unexpected connection_id\\. Previous=(.+?), New=(.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sdks/csharp/src/SpacetimeDBClient.cs","lineNumber":821,"sourceCode":"                            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);\n                            onConnect = null;\n                        }\n                    }\n                    catch (Exception e)\n                    {\n                        Log.Exception(e);\n                    }\n                    break;","sourceCodeStart":803,"sourceCodeEnd":839,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/524b4487d949b61a07d4f39c862d1290259dfd20/sdks/csharp/src/SpacetimeDBClient.cs#L803-L839","documentation":"Companion invariant to the identity check: InitialConnection must carry the same connection_id the client already stored; a different one throws InvalidOperationException inside the same try block (logged only, onConnect skipped). ConnectionId is assigned by the server per websocket session, so a change means the server (or an intermediary) presented a different session on the same client connection.","triggerScenarios":"A second InitialConnection message with a new connection_id — server-side session swap mid-connection, replayed frames from a proxy, or injected test messages after a first InitialConnection.","commonSituations":"Server restarts or internal reconnects reusing the client's websocket; test harnesses replaying recorded message streams on one connection.","solutions":["Treat the log line as a session integrity failure: disconnect and establish a fresh connection","Rebuild the DbConnection (new Build) whenever the underlying session may have changed rather than continuing with stale state","If you control the test harness, inject InitialConnection only once per connection"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat any duplicate InitialConnection as a session reset: disconnect and rebuild","In test harnesses, inject InitialConnection exactly once per connection","Watch client logs for the logged mismatch instead of waiting for onConnect timeouts"],"tags":["connection-id","reconnect","connection-lifecycle","csharp"],"backgroundTag":"connection-id-mismatch","analyzedSha":"524b4487d949b61a07d4f39c862d1290259dfd20","analyzedAt":"2026-08-16T23:58:54.611Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}