LykosAI/StabilityMatrix · error · InvalidOperationException

Comfy client is not connected

Error message

Comfy client is not connected

What it means

UploadAsPngAsync in ComfyImageUploadHelper requires an active ComfyUI API client to upload an image. The helper reads clientManager.Client, and throws InvalidOperationException when it is null, i.e. the Comfy client has not connected (or was disconnected/reset) before the upload was attempted. It is an internal state guard: the upload operation cannot proceed without a connected client.

Solutions

  1. Ensure the Comfy client is connected before generating: start ComfyUI and wait for the Connected state in ComfyClientManager.
  2. Await the manager's connection/initialization (e.g. await clientManager.ConnectAsync or the startup lifecycle) prior to calling UploadImagesAsync.
  3. If the server address changed, fix the Comfy host/port in Settings and reconnect.
  4. Add a guard that waits for or re-establishes the connection instead of calling the upload helper with a null Client.

Example fix

// before
await UploadImagesAsync(clientManager, streams, logger, ct);
// after
if (clientManager.Client is null)
    await clientManager.ConnectAsync(ct);
await UploadImagesAsync(clientManager, streams, logger, ct);
Defensive patterns

Strategy: type-guard

Validate before calling

if (clientManager.Client is null)
    throw new InvalidOperationException("Connect to ComfyUI before uploading images"); // or await connection first

Type guard

bool IsComfyConnected(ComfyClientManager m) => m.Client is not null;

Try / catch

try { await UploadImagesAsync(clientManager, streams, logger, ct); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Comfy client is not connected"))
{ logger.Warn("Comfy not connected; reconnecting..."); await clientManager.ConnectAsync(ct); }

Prevention

When it happens

Trigger: Calling UploadImagesAsync/UploadAsPngAsync while ComfyClientManager.Client is null — before the ComfyUI websocket/API client finished connecting, after a connection drop or reconnect reset, or when the Comfy backend is not running at all.

Common situations: Invoking generation (which uploads a mask/init image) right after app start before the client connects; ComfyUI server restarted or port changed so the client silently disconnected; a race where the upload task runs before the connection task completes.

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/756779a6fbdbb638. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Avalonia/Helpers/ComfyImageUploadHelper.cs:22

using StabilityMatrix.Core.Services.ImageGeneration;

namespace StabilityMatrix.Avalonia.Helpers;

/// <summary>
/// Helper for uploading images to ComfyUI for Image Lab providers
/// </summary>
public static class ComfyImageUploadHelper
{
    private static async Task UploadAsPngAsync(
        IInferenceClientManager clientManager,
        Stream sourceStream,
        string fileName,
        ILogger logger,
        CancellationToken cancellationToken
    )
    {
        var client =
            clientManager.Client ?? throw new InvalidOperationException("Comfy client is not connected");

        try
        {
            sourceStream.Position = 0;
        }
        catch (NotSupportedException)
        {
            // Stream is not seekable - continue with current position
        }

        try
        {
            using var bitmap = new Bitmap(sourceStream);
            await using var pngStream = new MemoryStream();
            bitmap.Save(pngStream);
            pngStream.Position = 0;

            await client.UploadImageAsync(pngStream, fileName, cancellationToken);

View on GitHub (pinned to af93d6ef57)