LykosAI/StabilityMatrix · error · InvalidOperationException

Client is not connected

Error message

Client is not connected

What it means

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.

Solutions

  1. Await ConnectAsync (or ensure the manager's connection is established) before calling any upload or inference method
  2. Check the IsConnected property as a precondition in your calling code and connect if false
  3. Verify the ComfyUI backend process is running and reachable at the configured address
  4. Recreate or reconnect the InferenceClientManager instance if a previous session was disposed

Example fix

// before
await clientManager.LoadSharedPropertiesAsync();
// after
if (!clientManager.IsConnected)
{
    await clientManager.ConnectAsync();
}
await clientManager.LoadSharedPropertiesAsync();
Defensive patterns

Strategy: try-catch

Validate before calling

if (!clientManager.IsConnected)
    throw new InvalidOperationException("Connect before calling inference APIs");

Type guard

bool ReadyForInference(InferenceClientManager m) => m is { IsConnected: true, Client: not null };

Try / catch

try
{
    await clientManager.UploadInputImageAsync(image, name);
}
catch (InvalidOperationException ex) when (ex.Message == "Client is not connected")
{
    await clientManager.ConnectAsync();
    // retry the operation once
}

Prevention

When it happens

Trigger: Calling LoadSharedPropertiesAsync, UploadInputImageAsync, or UploadMaskImageAsync (directly or via GenerateTextToImage/other inference helpers) before ConnectAsync succeeded, or after the client/server was disconnected or disposed.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/850c1c64a8736004. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Avalonia/Services/InferenceClientManager.cs:378

                        LoadSharedPropertiesAsync()
                            .SafeFireAndForget(onException: ex =>
                                logger.LogError(ex, "Error loading shared properties")
                            );
                    }
                }
                catch (Exception e)
                {
                    logger.LogError(e, "Error resetting shared properties for Inference");
                }
            });
        };
    }

    [MemberNotNull(nameof(Client))]
    private void EnsureConnected()
    {
        if (!IsConnected)
            throw new InvalidOperationException("Client is not connected");
    }

    protected virtual async Task LoadSharedPropertiesAsync()
    {
        EnsureConnected();

        // Get model names
        if (await Client.GetModelNamesAsync() is { } modelNames)
        {
            // Build the checkpoint list (backend-reported models, preferring the local index
            // entry for its richer metadata) and apply it as a single diff. Doing this in one
            // pass — rather than resetting to local-only and then re-adding remote — avoids
            // transiently removing remote-only models, which resets the scroll position of an
            // open model dropdown while the list refreshes.
            var localModelsById = modelIndexService
                .FindByModelType(SharedFolderType.StableDiffusion)
                .Select(HybridModelFile.FromLocal)
                .GroupBy(m => m.GetId())

View on GitHub (pinned to af93d6ef57)