{"record":{"id":"850c1c64a8736004","repo":"LykosAI/StabilityMatrix","slug":"client-is-not-connected","errorCode":null,"errorMessage":"Client is not connected","messagePattern":"Client is not connected","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Avalonia/Services/InferenceClientManager.cs","lineNumber":378,"sourceCode":"                        LoadSharedPropertiesAsync()\n                            .SafeFireAndForget(onException: ex =>\n                                logger.LogError(ex, \"Error loading shared properties\")\n                            );\n                    }\n                }\n                catch (Exception e)\n                {\n                    logger.LogError(e, \"Error resetting shared properties for Inference\");\n                }\n            });\n        };\n    }\n\n    [MemberNotNull(nameof(Client))]\n    private void EnsureConnected()\n    {\n        if (!IsConnected)\n            throw new InvalidOperationException(\"Client is not connected\");\n    }\n\n    protected virtual async Task LoadSharedPropertiesAsync()\n    {\n        EnsureConnected();\n\n        // Get model names\n        if (await Client.GetModelNamesAsync() is { } modelNames)\n        {\n            // Build the checkpoint list (backend-reported models, preferring the local index\n            // entry for its richer metadata) and apply it as a single diff. Doing this in one\n            // pass — rather than resetting to local-only and then re-adding remote — avoids\n            // transiently removing remote-only models, which resets the scroll position of an\n            // open model dropdown while the list refreshes.\n            var localModelsById = modelIndexService\n                .FindByModelType(SharedFolderType.StableDiffusion)\n                .Select(HybridModelFile.FromLocal)\n                .GroupBy(m => m.GetId())","sourceCodeStart":360,"sourceCodeEnd":396,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Avalonia/Services/InferenceClientManager.cs#L360-L396","documentation":"InferenceClientManager throws this InvalidOperationException from EnsureConnected when any operation that requires a live ComfyUI/SharedModel client is attempted while IsConnected is false. The [MemberNotNull] attribute means the compiler assumes Client is non-null afterward; calling without a connection breaks that invariant. It is a state precondition guard, not a transient failure.","triggerScenarios":"Calling LoadSharedPropertiesAsync, UploadInputImageAsync, or UploadMaskImageAsync (directly or via GenerateTextToImage/other inference helpers) before ConnectAsync succeeded, or after the client/server was disconnected or disposed.","commonSituations":"ComfyUI server not started or crashed before inference; calling upload/inference methods before awaiting ConnectAsync; connection dropped between check and use; using a stale manager instance after package restart.","solutions":["Await ConnectAsync (or ensure the manager's connection is established) before calling any upload or inference method","Check the IsConnected property as a precondition in your calling code and connect if false","Verify the ComfyUI backend process is running and reachable at the configured address","Recreate or reconnect the InferenceClientManager instance if a previous session was disposed"],"exampleFix":"// before\nawait clientManager.LoadSharedPropertiesAsync();\n// after\nif (!clientManager.IsConnected)\n{\n    await clientManager.ConnectAsync();\n}\nawait clientManager.LoadSharedPropertiesAsync();","handlingStrategy":"try-catch","validationCode":"if (!clientManager.IsConnected)\n    throw new InvalidOperationException(\"Connect before calling inference APIs\");","typeGuard":"bool ReadyForInference(InferenceClientManager m) => m is { IsConnected: true, Client: not null };","tryCatchPattern":"try\n{\n    await clientManager.UploadInputImageAsync(image, name);\n}\ncatch (InvalidOperationException ex) when (ex.Message == \"Client is not connected\")\n{\n    await clientManager.ConnectAsync();\n    // retry the operation once\n}","preventionTips":["Always await ConnectAsync before any upload/inference call","Check IsConnected as a cheap precondition","Handle server lifecycle events to detect disconnects early","Avoid reusing manager instances after disposal"],"tags":["invalid-state","connection","inference"],"backgroundTag":"invalid-state-transition","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}