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
- 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
Defensive patterns
Strategy: validation
Prevention
- 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
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
- Received InitialConnection with unexpected identity. Previou
- Expected ConnectionId hex string to be 32 characters long, b
- Argument must be a ConnectionId
- Reducer result for unknown request_id {reducerResult.Request
- Identity not set
AI-assisted analysis of clockworklabs/SpacetimeDB@524b4487d9 (2026-08-16).
Data as JSON: /api/errors/67d176782a33e224.
Report an issue: GitHub.