clockworklabs/SpacetimeDB · error · InvalidOperationException

Received InitialConnection with unexpected identity. Previou

Error message

Received InitialConnection with unexpected identity. Previous={identity}, New={initialConnection.Identity}

What it means

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.

Source

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

                        {
                            var legacyEventContext = ToEventContext(new Event<Reducer>.Reducer(reducerEvent));
                            ApplyUpdate(legacyEventContext, dbOps);
                            var eventContext = ToReducerEventContext(reducerEvent);
                            Dispatch(eventContext, reducerEvent.Reducer);
                        }
                        else
                        {
                            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);

View on GitHub (pinned to 524b4487d9)

Solutions

  1. When the auth token changes, Disconnect and build a new DbConnection instead of reusing the live one
  2. Check client logs for the Log.Exception entry — an app stuck after connect usually points here
  3. Verify proxies/LBs in front of the server are not replaying websocket traffic
Defensive patterns

Strategy: validation

Validate before calling

// If you must change tokens, verify and rebuild rather than reusing the session:
if (tokenChanged)
{
    conn.Disconnect();
    conn = NewBuilder().WithUri(uri).WithDatabaseName(db).WithToken(newToken).Build();
}

Prevention

When it happens

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

Common situations: Auth-token refresh implemented by reconnecting on the same live connection; misbehaving intermediaries duplicating frames; integration tests feeding canned message sequences.

Related errors


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