{"record":{"id":"f82809718b4efc24","repo":"babalae/better-genshin-impact","slug":"error-f82809","errorCode":null,"errorMessage":"根实例未取得首个命名管道服务端。","messagePattern":"根实例未取得首个命名管道服务端。","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"BetterGenshinImpact/Service/Instance/InstanceService.cs","lineNumber":90,"sourceCode":"\n    public event EventHandler<WebViewMessageReceivedEventArgs>? WebViewMessageReceived;\n\n    public InstanceContext Context => _bootstrap.Context;\n\n    public bool IsGameMouseModeEnabled =>\n        _relativeMouseMessageHandler.IsGameMouseModeEnabled;\n\n    public void SetGameMouseModeEnabled(bool enabled)\n    {\n        _relativeMouseMessageHandler.SetGameMouseModeEnabled(enabled);\n    }\n\n    public Task StartAsync(CancellationToken cancellationToken)\n    {\n        if (Context.InstanceType == BetterGiInstanceType.Primary)\n        {\n            var firstServer = _bootstrap.TakeFirstServer()\n                              ?? throw new InvalidOperationException(\n                                  \"根实例未取得首个命名管道服务端。\");\n            _acceptLoopTask = AcceptLoopAsync(\n                firstServer,\n                _lifetimeCancellationTokenSource.Token);\n        }\n        else\n        {\n            _rootConnectionLoopTask = RootConnectionLoopAsync(\n                _bootstrap.TakeFirstRootConnection(),\n                _lifetimeCancellationTokenSource.Token);\n        }\n\n        _logger.LogInformation(\n            \"实例 IPC v2 已启动：{InstanceType}，进程 {ProcessId}，Session {SessionId}，根管道 {PipeName}\",\n            Context.InstanceType,\n            Context.ProcessId,\n            Context.WindowsSessionId,\n            Context.RootPipeName);","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/Service/Instance/InstanceService.cs#L72-L108","documentation":"Thrown in InstanceService.StartAsync when the process is the Primary instance but _bootstrap.TakeFirstServer() returns null. TakeFirstServer atomically transfers the first NamedPipeServerStream (created during InstanceBootstrap.Initialize) to InstanceService exactly once; returning null means no server was ever created or it was already consumed.","triggerScenarios":"InstanceBootstrap.Initialize determined this process should be Primary (won the FirstPipeInstance race) but failed to create or retain the first server stream — possibly due to a pipe creation error that was swallowed, or StartAsync was called twice (second TakeFirstServer returns null). Alternatively, the bootstrap was never initialized before StartAsync ran, or initialization logic has a bug where the Primary path doesn't populate _firstServer.","commonSituations":"Startup ordering bug where InstanceService.StartAsync runs before InstanceBootstrap.Initialize completes; a race where the bootstrap disposed the server during ActivationForwarded processing but InstanceType was still Primary; calling StartAsync more than once; a DI misconfiguration that creates two InstanceService instances both calling TakeFirstServer.","solutions":["Verify InstanceBootstrap.Initialize() is called and fully completed before InstanceService.StartAsync — check the startup sequence in App.xaml.cs or the host builder.","Ensure StartAsync is only called once by the generic host (it should be — check for manual invocations).","Inspect InstanceBootstrap.Initialize for the Primary branch to confirm it always assigns _firstServer via InstancePipeFactory.CreateServer before returning.","If the error recurs, add logging in InstanceBootstrap.Initialize to confirm the Primary path executed and _firstServer was set."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Before StartAsync, verify bootstrap state\nif (Context.InstanceType == BetterGiInstanceType.Primary && bootstrap is { } b)\n{\n    // TakeFirstServer is one-shot; ensure it hasn't been called yet\n    // by verifying Initialize completed successfully\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure InstanceBootstrap.Initialize() fully completes before InstanceService.StartAsync runs.","Never call StartAsync twice on the same InstanceService.","Verify the DI container registers InstanceBootstrap and InstanceService as singletons.","Check that InstanceBootstrap.Initialize's Primary branch creates the server before any ActivationForwarded exit path."],"tags":["startup","ipc","named-pipe","bootstrap","initialization-order"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}