{"record":{"id":"756779a6fbdbb638","repo":"LykosAI/StabilityMatrix","slug":"comfy-client-is-not-connected","errorCode":null,"errorMessage":"Comfy client is not connected","messagePattern":"Comfy client is not connected","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Avalonia/Helpers/ComfyImageUploadHelper.cs","lineNumber":22,"sourceCode":"using StabilityMatrix.Core.Services.ImageGeneration;\n\nnamespace StabilityMatrix.Avalonia.Helpers;\n\n/// <summary>\n/// Helper for uploading images to ComfyUI for Image Lab providers\n/// </summary>\npublic static class ComfyImageUploadHelper\n{\n    private static async Task UploadAsPngAsync(\n        IInferenceClientManager clientManager,\n        Stream sourceStream,\n        string fileName,\n        ILogger logger,\n        CancellationToken cancellationToken\n    )\n    {\n        var client =\n            clientManager.Client ?? throw new InvalidOperationException(\"Comfy client is not connected\");\n\n        try\n        {\n            sourceStream.Position = 0;\n        }\n        catch (NotSupportedException)\n        {\n            // Stream is not seekable - continue with current position\n        }\n\n        try\n        {\n            using var bitmap = new Bitmap(sourceStream);\n            await using var pngStream = new MemoryStream();\n            bitmap.Save(pngStream);\n            pngStream.Position = 0;\n\n            await client.UploadImageAsync(pngStream, fileName, cancellationToken);","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Avalonia/Helpers/ComfyImageUploadHelper.cs#L4-L40","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the Comfy client is connected before generating: start ComfyUI and wait for the Connected state in ComfyClientManager.","Await the manager's connection/initialization (e.g. await clientManager.ConnectAsync or the startup lifecycle) prior to calling UploadImagesAsync.","If the server address changed, fix the Comfy host/port in Settings and reconnect.","Add a guard that waits for or re-establishes the connection instead of calling the upload helper with a null Client."],"exampleFix":"// before\nawait UploadImagesAsync(clientManager, streams, logger, ct);\n// after\nif (clientManager.Client is null)\n    await clientManager.ConnectAsync(ct);\nawait UploadImagesAsync(clientManager, streams, logger, ct);","handlingStrategy":"type-guard","validationCode":"if (clientManager.Client is null)\n    throw new InvalidOperationException(\"Connect to ComfyUI before uploading images\"); // or await connection first","typeGuard":"bool IsComfyConnected(ComfyClientManager m) => m.Client is not null;","tryCatchPattern":"try { await UploadImagesAsync(clientManager, streams, logger, ct); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Comfy client is not connected\"))\n{ logger.Warn(\"Comfy not connected; reconnecting...\"); await clientManager.ConnectAsync(ct); }","preventionTips":["Await the client connection lifecycle before any generation/upload call.","Guard all Comfy calls behind an IsConnected check.","Subscribe to disconnect events and disable generate buttons until reconnected."],"tags":["comfyui","client-not-connected","invalid-state","async-race"],"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"}