babalae/better-genshin-impact · error · InvalidOperationException

根实例分配了不匹配的客户端类型:{openResponse.AssignedType}。

Error message

根实例分配了不匹配的客户端类型:{openResponse.AssignedType}。

What it means

Thrown in RootConnectionLoopAsync when the connection.open response's AssignedType field does not match the child's requested Context.InstanceType. The root has authority to assign instance types — but in normal operation it should echo back the requested type (or ActivationForwarded). A mismatch means the root decided this process should play a different role than it was launched as.

Source

Thrown at BetterGenshinImpact/Service/Instance/InstanceService.cs:443

                        },
                        RequestTimeout,
                        cancellationToken).ConfigureAwait(false);
                    EnsureSuccessfulResponse(openResult);
                    openResponse =
                        openResult.Data?.ToObject<ConnectionOpenResponse>(
                            InstanceIpcProtocol.Serializer)
                        ?? throw new InvalidDataException("根实例连接响应缺少数据。");
                }

                if (openResponse.Disposition
                    == ConnectionOpenDisposition.ActivationForwarded)
                {
                    RequestApplicationShutdown();
                    return;
                }
                if (openResponse.AssignedType != Context.InstanceType)
                {
                    throw new InvalidOperationException(
                        $"根实例分配了不匹配的客户端类型:{openResponse.AssignedType}。");
                }

                Context.SetRootSessionId(openResponse.RootSessionId);
                connection.RemoteEndpoint = new InstanceEndpoint
                {
                    InstanceType = BetterGiInstanceType.Primary,
                    ProcessId = openResponse.RootProcessId,
                    WindowsSessionId = openResponse.RootSessionId,
                    StartedAt = DateTimeOffset.UtcNow
                };
                if (!connection.IsStarted)
                {
                    connection.Start(cancellationToken);
                }
                if (connection.Completion.IsCompleted)
                {
                    throw new IOException("根实例连接在完成登记前已经关闭。");

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Verify both processes run the same application version to eliminate schema deserialization skew.
  2. Check HandleConnectionOpenAsync and CreateOpenResponse on the root side to confirm AssignedType always matches the request's RequestedType for Accepted dispositions.
  3. Log openResponse.AssignedType and Context.InstanceType values at the throw site to identify which type was unexpectedly assigned.
  4. If this occurs after a protocol change, ensure ConnectionOpenResponse.AssignedType maps correctly to the BetterGiInstanceType enum.
Defensive patterns

Strategy: try-catch

Try / catch

// Already handled in RootConnectionLoopAsync:
catch (Exception exception) when (exception is InvalidOperationException or InvalidDataException)
{
    _logger.LogError(exception, "连接协议错误,停止重连:{PipeName}", Context.RootPipeName);
    RequestApplicationShutdown();
    return;
}

Prevention

When it happens

Trigger: The child sent ConnectionOpenRequest.RequestedType = Context.InstanceType (e.g., ChildSession), but the root's response has AssignedType set to a different value (e.g., Primary or WebView). Looking at HandleConnectionOpenAsync, the root always echoes the requested type for Accepted connections, so a mismatch indicates either a version skew in the response model or an unexpected root-side code path.

Common situations: Protocol version mismatch where ConnectionOpenResponse fields are misaligned during deserialization (AssignedType reads garbage); the root instance has a bug in CreateOpenResponse that assigns the wrong type; a race where the root's context changed between receiving and responding; test fixtures that construct responses with mismatched types.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/a43333e533ff3e15. Report an issue: GitHub.