From 3bfa4c9afd77d07ab5912348797b1e0d01a9b473 Mon Sep 17 00:00:00 2001 From: Deepratna Awale Date: Sat, 7 Nov 2026 10:55:00 +0000 Subject: [PATCH 1/2] Stop logging unhandled ComfyUI websocket messages at Warn HandleTextMessage only acts on executing, status, progress and execution_error. Every other message fell through to a Warn that included the full pretty-printed JSON payload, so normal ComfyUI traffic (execution_start, execution_cached, executed, execution_success, progress_state) and custom node broadcasts such as resource monitors that send a message every second filled app.log. Recognise the standard ComfyUI message types the client doesn't use and skip them silently, and log any other type at Trace with only its type. The full message is still available at Trace from the existing "Received json message" line. Invalid JSON is still logged at Warn. --- StabilityMatrix.Core/Inference/ComfyClient.cs | 18 +++- .../Api/Comfy/ComfyWebSocketResponseType.cs | 9 ++ .../Core/ComfyClientMessageLoggingTests.cs | 92 +++++++++++++++++++ 3 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 StabilityMatrix.Tests/Core/ComfyClientMessageLoggingTests.cs diff --git a/StabilityMatrix.Core/Inference/ComfyClient.cs b/StabilityMatrix.Core/Inference/ComfyClient.cs index 197adde6e..c65334106 100644 --- a/StabilityMatrix.Core/Inference/ComfyClient.cs +++ b/StabilityMatrix.Core/Inference/ComfyClient.cs @@ -148,7 +148,7 @@ private void OnMessageReceived(ResponseMessage message) } } - private void HandleTextMessage(string text) + internal void HandleTextMessage(string text) { ComfyWebSocketResponse? json; try @@ -254,9 +254,23 @@ is not { } errorData Logger.Warn($"Could not find task for prompt {errorData.PromptId}, skipping"); } } + else if ( + json.Type + is ComfyWebSocketResponseType.ExecutionStart + or ComfyWebSocketResponseType.ExecutionCached + or ComfyWebSocketResponseType.Executed + or ComfyWebSocketResponseType.ExecutionSuccess + or ComfyWebSocketResponseType.ExecutionInterrupted + or ComfyWebSocketResponseType.ProgressState + ) + { + // Standard ComfyUI messages this client doesn't use + } else { - Logger.Warn($"Unknown message type {json.Type} ({json.Data}), skipping"); + // Includes custom node broadcasts (e.g. resource monitors) which can arrive every second, + // full message content is already logged at Trace above + Logger.Trace("Skipping unhandled message type {Type}", json.Type); } } diff --git a/StabilityMatrix.Core/Models/Api/Comfy/ComfyWebSocketResponseType.cs b/StabilityMatrix.Core/Models/Api/Comfy/ComfyWebSocketResponseType.cs index 7cbe40b8e..c96255780 100644 --- a/StabilityMatrix.Core/Models/Api/Comfy/ComfyWebSocketResponseType.cs +++ b/StabilityMatrix.Core/Models/Api/Comfy/ComfyWebSocketResponseType.cs @@ -29,4 +29,13 @@ public enum ComfyWebSocketResponseType [EnumMember(Value = "executed")] Executed, + + [EnumMember(Value = "execution_success")] + ExecutionSuccess, + + [EnumMember(Value = "execution_interrupted")] + ExecutionInterrupted, + + [EnumMember(Value = "progress_state")] + ProgressState, } diff --git a/StabilityMatrix.Tests/Core/ComfyClientMessageLoggingTests.cs b/StabilityMatrix.Tests/Core/ComfyClientMessageLoggingTests.cs new file mode 100644 index 000000000..23cce0ff7 --- /dev/null +++ b/StabilityMatrix.Tests/Core/ComfyClientMessageLoggingTests.cs @@ -0,0 +1,92 @@ +using System.Runtime.CompilerServices; +using System.Text.Json; +using NLog; +using NLog.Config; +using NLog.Targets; +using StabilityMatrix.Core.Inference; +using StabilityMatrix.Core.Models.Api.Comfy; + +namespace StabilityMatrix.Tests.Core; + +[TestClass] +[DoNotParallelize] +public class ComfyClientMessageLoggingTests +{ + private LoggingConfiguration? previousConfiguration; + private MemoryTarget memory = null!; + + [TestInitialize] + public void Setup() + { + previousConfiguration = LogManager.Configuration; + + memory = new MemoryTarget("memory") { Layout = "${level}|${message}" }; + var config = new LoggingConfiguration(); + config.AddRule(LogLevel.Trace, LogLevel.Fatal, memory, typeof(ComfyClient).FullName!); + LogManager.Configuration = config; + } + + [TestCleanup] + public void Cleanup() + { + LogManager.Configuration = previousConfiguration; + } + + private static ComfyClient CreateClient() => + (ComfyClient)RuntimeHelpers.GetUninitializedObject(typeof(ComfyClient)); + + private string[] LogsAtOrAbove(LogLevel level) => + memory.Logs.Where(l => LogLevel.FromString(l.Split('|')[0]) >= level).ToArray(); + + [DataTestMethod] + [DataRow("""{"type":"execution_start","data":{"prompt_id":"p1","timestamp":1}}""")] + [DataRow("""{"type":"execution_cached","data":{"nodes":["1","2"],"prompt_id":"p1"}}""")] + [DataRow("""{"type":"executed","data":{"node":"9","output":{"images":[]},"prompt_id":"p1"}}""")] + [DataRow("""{"type":"execution_success","data":{"prompt_id":"p1","timestamp":2}}""")] + [DataRow("""{"type":"execution_interrupted","data":{"prompt_id":"p1","node_id":"3"}}""")] + [DataRow( + """{"type":"progress_state","data":{"prompt_id":"p1","nodes":{"3":{"value":1,"max":20,"state":"running"}}}}""" + )] + [DataRow("""{"type":"crystools.monitor","data":{"cpu_utilization":12.5}}""")] + public void HandleTextMessage_UnhandledTypes_DoNotLogAboveTrace(string message) + { + CreateClient().HandleTextMessage(message); + LogManager.Flush(); + + CollectionAssert.AreEqual(Array.Empty(), LogsAtOrAbove(LogLevel.Debug)); + } + + [TestMethod] + public void HandleTextMessage_CustomNodeType_LogsTypeWithoutPayloadAtTrace() + { + CreateClient().HandleTextMessage("""{"type":"crystools.monitor","data":{"cpu_utilization":12.5}}"""); + LogManager.Flush(); + + var skipped = memory.Logs.Single(l => l.Contains("Skipping unhandled message type")); + StringAssert.StartsWith(skipped, "Trace|"); + Assert.IsFalse(skipped.Contains("cpu_utilization")); + } + + [TestMethod] + public void HandleTextMessage_InvalidJson_StillWarns() + { + CreateClient().HandleTextMessage("not json"); + LogManager.Flush(); + + Assert.AreEqual(1, LogsAtOrAbove(LogLevel.Warn).Length); + } + + [DataTestMethod] + [DataRow("execution_success", ComfyWebSocketResponseType.ExecutionSuccess)] + [DataRow("execution_interrupted", ComfyWebSocketResponseType.ExecutionInterrupted)] + [DataRow("progress_state", ComfyWebSocketResponseType.ProgressState)] + [DataRow("crystools.monitor", ComfyWebSocketResponseType.Unknown)] + public void ResponseType_DeserializesNewTypes(string type, ComfyWebSocketResponseType expected) + { + var response = JsonSerializer.Deserialize( + $$$"""{"type":"{{{type}}}","data":{}}""" + ); + + Assert.AreEqual(expected, response!.Type); + } +} From 8d5dbea43baf77753484d1f67fa8148c03c57428 Mon Sep 17 00:00:00 2001 From: Deepratna Awale Date: Wed, 11 Nov 2026 20:18:51 +0000 Subject: [PATCH 2/2] Log the raw type of unhandled ComfyUI messages and keep interrupts at Debug Custom node message types all deserialize to Unknown, so read the type string from the message when logging it at Trace. Log execution_interrupted at Debug with its prompt and node ids, since it helps when diagnosing a generation that stopped. --- StabilityMatrix.Core/Inference/ComfyClient.cs | 32 +++++++++++++++++-- .../Core/ComfyClientMessageLoggingTests.cs | 18 ++++++++++- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/StabilityMatrix.Core/Inference/ComfyClient.cs b/StabilityMatrix.Core/Inference/ComfyClient.cs index c65334106..6c71f1a09 100644 --- a/StabilityMatrix.Core/Inference/ComfyClient.cs +++ b/StabilityMatrix.Core/Inference/ComfyClient.cs @@ -260,20 +260,46 @@ is ComfyWebSocketResponseType.ExecutionStart or ComfyWebSocketResponseType.ExecutionCached or ComfyWebSocketResponseType.Executed or ComfyWebSocketResponseType.ExecutionSuccess - or ComfyWebSocketResponseType.ExecutionInterrupted or ComfyWebSocketResponseType.ProgressState ) { // Standard ComfyUI messages this client doesn't use } - else + else if (json.Type == ComfyWebSocketResponseType.ExecutionInterrupted) + { + // Kept at Debug to help diagnose reports of generations that stopped + Logger.Debug( + "Execution interrupted (PromptId = {PromptId}, NodeId = {NodeId})", + json.Data["prompt_id"]?.ToString(), + json.Data["node_id"]?.ToString() + ); + } + else if (Logger.IsTraceEnabled) { // Includes custom node broadcasts (e.g. resource monitors) which can arrive every second, // full message content is already logged at Trace above - Logger.Trace("Skipping unhandled message type {Type}", json.Type); + Logger.Trace( + "Skipping unhandled message type {Type}", + GetRawMessageType(text) ?? json.Type.ToString() + ); } } + /// + /// Returns the "type" string of a websocket message as sent, since types this client + /// doesn't know (such as custom node broadcasts) all deserialize to + /// . + /// + private static string? GetRawMessageType(string text) + { + using var document = JsonDocument.Parse(text); + return + document.RootElement.TryGetProperty("type", out var type) + && type.ValueKind == JsonValueKind.String + ? type.GetString() + : null; + } + /// /// Parses binary data (previews) into image streams /// https://github.com/comfyanonymous/ComfyUI/blob/master/server.py#L518 diff --git a/StabilityMatrix.Tests/Core/ComfyClientMessageLoggingTests.cs b/StabilityMatrix.Tests/Core/ComfyClientMessageLoggingTests.cs index 23cce0ff7..008b72e48 100644 --- a/StabilityMatrix.Tests/Core/ComfyClientMessageLoggingTests.cs +++ b/StabilityMatrix.Tests/Core/ComfyClientMessageLoggingTests.cs @@ -43,7 +43,6 @@ private string[] LogsAtOrAbove(LogLevel level) => [DataRow("""{"type":"execution_cached","data":{"nodes":["1","2"],"prompt_id":"p1"}}""")] [DataRow("""{"type":"executed","data":{"node":"9","output":{"images":[]},"prompt_id":"p1"}}""")] [DataRow("""{"type":"execution_success","data":{"prompt_id":"p1","timestamp":2}}""")] - [DataRow("""{"type":"execution_interrupted","data":{"prompt_id":"p1","node_id":"3"}}""")] [DataRow( """{"type":"progress_state","data":{"prompt_id":"p1","nodes":{"3":{"value":1,"max":20,"state":"running"}}}}""" )] @@ -64,9 +63,26 @@ public void HandleTextMessage_CustomNodeType_LogsTypeWithoutPayloadAtTrace() var skipped = memory.Logs.Single(l => l.Contains("Skipping unhandled message type")); StringAssert.StartsWith(skipped, "Trace|"); + StringAssert.Contains(skipped, "crystools.monitor"); Assert.IsFalse(skipped.Contains("cpu_utilization")); } + [TestMethod] + public void HandleTextMessage_ExecutionInterrupted_LogsAtDebug() + { + CreateClient() + .HandleTextMessage( + """{"type":"execution_interrupted","data":{"prompt_id":"p1","node_id":"3","node_type":"KSampler"}}""" + ); + LogManager.Flush(); + + var logs = LogsAtOrAbove(LogLevel.Debug); + Assert.AreEqual(1, logs.Length); + StringAssert.StartsWith(logs[0], "Debug|"); + StringAssert.Contains(logs[0], "p1"); + StringAssert.Contains(logs[0], "3"); + } + [TestMethod] public void HandleTextMessage_InvalidJson_StillWarns() {