clockworklabs/SpacetimeDB · error · InvalidOperationException

Received InitialConnection with unexpected connection_id. Pr

Error message

Received InitialConnection with unexpected connection_id. Previous={connectionId}, New={initialConnection.ConnectionId}

What it means

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.

Source

Thrown at sdks/csharp/src/SpacetimeDBClient.cs:821

                            var legacyEventContext = ToEventContext(new Event<Reducer>.UnknownTransaction());
                            ApplyUpdate(legacyEventContext, dbOps);
                        }
                        break;
                    }
                case ServerMessage.InitialConnection(var initialConnection):
                    try
                    {
                        if (Identity is Identity identity && identity != initialConnection.Identity)
                        {
                            throw new InvalidOperationException(
                                $"Received InitialConnection with unexpected identity. Previous={identity}, New={initialConnection.Identity}"
                            );
                        }

                        if (initialConnectionId is ConnectionId connectionId
                            && connectionId != initialConnection.ConnectionId)
                        {
                            throw new InvalidOperationException(
                                $"Received InitialConnection with unexpected connection_id. Previous={connectionId}, New={initialConnection.ConnectionId}"
                            );
                        }

                        Identity = initialConnection.Identity;
                        initialConnectionId = initialConnection.ConnectionId;
                        if (!onConnectInvoked)
                        {
                            onConnectInvoked = true;
                            onConnect?.Invoke(initialConnection.Identity, initialConnection.Token);
                            onConnect = null;
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Exception(e);
                    }
                    break;

View on GitHub (pinned to 524b4487d9)

Solutions

  1. Treat the log line as a session integrity failure: disconnect and establish a fresh connection
  2. Rebuild the DbConnection (new Build) whenever the underlying session may have changed rather than continuing with stale state
  3. If you control the test harness, inject InitialConnection only once per connection
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: 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.

Common situations: Server restarts or internal reconnects reusing the client's websocket; test harnesses replaying recorded message streams on one connection.

Related errors


AI-assisted analysis of clockworklabs/SpacetimeDB@524b4487d9 (2026-08-16). Data as JSON: /api/errors/67d176782a33e224. Report an issue: GitHub.