{"record":{"id":"1db09bbc310dbbdf","repo":"babalae/better-genshin-impact","slug":"error-1db09b","errorCode":null,"errorMessage":"只有根实例可以接受客户端连接登记。","messagePattern":"只有根实例可以接受客户端连接登记。","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/Service/Instance/MessageHandlers/InstanceRequestHandler.cs","lineNumber":141,"sourceCode":"            ?? throw new ArgumentException(\"激活请求缺少命令行参数。\");\n        _enqueueActivation(activation.Arguments);\n        return CacheActivationResponse(\n            request.RequestId,\n            InstanceIpcEnvelope.Response(request));\n    }\n\n    /// <summary>\n    /// 校验子实例身份和启动记录后，将当前连接登记为有效子连接。\n    /// v2 不再校验父实例 ID 或启动记录，而是使用根管道客户端的真实 PID 和 Session。\n    /// </summary>\n    private async Task<InstanceIpcEnvelope> HandleConnectionOpenAsync(\n        InstanceConnection connection,\n        InstanceIpcEnvelope request,\n        CancellationToken cancellationToken)\n    {\n        if (_context.InstanceType != BetterGiInstanceType.Primary)\n        {\n            throw new InvalidOperationException(\"只有根实例可以接受客户端连接登记。\");\n        }\n        if (connection.RemoteEndpoint is not null)\n        {\n            throw new InvalidOperationException(\"当前管道连接已经完成登记。\");\n        }\n        if (connection.ClientProcessId is not { } processId\n            || connection.ClientSessionId is not { } sessionId)\n        {\n            throw new InvalidOperationException(\"无法取得命名管道客户端的进程或 Session 信息。\");\n        }\n\n        var open =\n            request.Data?.ToObject<ConnectionOpenRequest>(InstanceIpcProtocol.Serializer)\n            ?? throw new ArgumentException(\"连接登记请求缺少数据。\");\n        if (open.RequestedType == BetterGiInstanceType.WebView)\n        {\n            var endpoint = CreateEndpoint(\n                BetterGiInstanceType.WebView,","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/Service/Instance/MessageHandlers/InstanceRequestHandler.cs#L123-L159","documentation":"Thrown by HandleConnectionOpenAsync when the current instance's type is not Primary. Only the root (Primary) instance is authorized to accept client connection registrations — it owns the registration state (_state.BetterGiConnectionsBySession, _state.WebViewConnectionsByProcessId). A ChildSession or WebView receiving a connection.open request indicates the IPC topology is inverted or a peer connected to the wrong pipe.","triggerScenarios":"A non-Primary instance receives an InstanceOperations.ConnectionOpen request. This should never happen in normal operation because clients connect to the root pipe (BetterGI.v2.user-<SID>.root) which is only owned by the Primary. It could occur if two processes both believe they are clients but one somehow acts as a server, or if pipe name resolution is wrong and a client connects to a child's pipe instead of the root's.","commonSituations":"A pipe name collision or misconfiguration where a child process's pipe has the same name as the root's; a version mismatch in pipe naming logic (InstancePipeNames); a malicious or buggy peer connecting to the wrong endpoint; a test environment where InstanceContext was set up incorrectly.","solutions":["Verify InstancePipeNames.ForCurrentUser() produces consistent names across all processes — it derives from WindowsIdentity SID, so ensure no impersonation or SID resolution differences.","Ensure only the Primary process creates a NamedPipeServerStream with FirstPipeInstance — check InstanceBootstrap.Initialize logic.","Catch this error on the child side and log the pipe name and remote process to diagnose the misconnection.","If running tests, ensure InstanceContext.InstanceType is set correctly for each test process."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Only the Primary instance should handle connection.open\n// This is enforced inside HandleConnectionOpenAsync;\n// callers should ensure this handler is only registered on Primary.","typeGuard":"// Check if this instance is authorized to accept connections\nstatic bool CanAcceptConnections(InstanceContext context)\n    => context.InstanceType == BetterGiInstanceType.Primary;","tryCatchPattern":null,"preventionTips":["Ensure pipe naming (InstancePipeNames) is consistent across all processes.","Only the Primary process should create a server with FirstPipeInstance.","Verify InstanceContext.InstanceType is set correctly at bootstrap."],"tags":["ipc","connection-handshake","authorization","instance-type","pipe-naming"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}