diff --git a/internal/comfy/gate.go b/internal/comfy/gate.go index d6c2cfd..8d22fa8 100644 --- a/internal/comfy/gate.go +++ b/internal/comfy/gate.go @@ -13,6 +13,7 @@ import ( "net/url" "os" "os/exec" + "os/signal" "path/filepath" "regexp" "strconv" @@ -112,12 +113,29 @@ func RunGate(ctx context.Context, cfg GateConfig) error { if out == nil { out = os.Stdout } - child.Stdout, child.Stderr = out, out + // ComfyUI writes to a pipe of the gate's, never to what the gate was + // given. That is the agent's, and an agent that restarts and adopts the + // replica leaves it with no reader: ComfyUI's next progress bar would + // fail with a broken pipe, and take the workflow it belongs to with it. + // The gate reads the pipe for as long as ComfyUI writes, and passes on + // what it can. + signal.Ignore(syscall.SIGPIPE) + pr, err := child.StdoutPipe() + if err != nil { + return err + } + child.Stderr = child.Stdout if err := child.Start(); err != nil { return fmt.Errorf("starting ComfyUI: %w", err) } + relayed := make(chan struct{}) + go func() { relay(pr, out); close(relayed) }() exited := make(chan error, 1) - go func() { exited <- child.Wait() }() + go func() { + // Wait closes the pipe; what is in it is read first. + <-relayed + exited <- child.Wait() + }() upstream := &url.URL{Scheme: "http", Host: net.JoinHostPort("127.0.0.1", strconv.Itoa(inner))} srv := &http.Server{ @@ -296,3 +314,21 @@ func freePort() (int, error) { defer l.Close() return l.Addr().(*net.TCPAddr).Port, nil } + +// relay copies r to w for as long as r has anything, and goes on reading +// after w fails: a writer that stops reading must not stop the one writing. +func relay(r io.Reader, w io.Writer) { + buf := make([]byte, 32<<10) + broken := false + for { + n, err := r.Read(buf) + if n > 0 && !broken { + if _, werr := w.Write(buf[:n]); werr != nil { + broken = true + } + } + if err != nil { + return + } + } +} diff --git a/internal/comfy/gate_test.go b/internal/comfy/gate_test.go index fdeabad..9bd1383 100644 --- a/internal/comfy/gate_test.go +++ b/internal/comfy/gate_test.go @@ -2,6 +2,7 @@ package comfy import ( "context" + "errors" "io" "net" "net/http" @@ -125,7 +126,14 @@ func fakeComfyUI() { } } _ = os.WriteFile(filepath.Join(base, "args"), []byte(strings.Join(os.Args[1:], " ")+"\nkey="+os.Getenv("LLMGW_ENGINE_KEY")), 0o644) - http.HandleFunc("/system_stats", func(w http.ResponseWriter, _ *http.Request) { _, _ = io.WriteString(w, "{}") }) + http.HandleFunc("/system_stats", func(w http.ResponseWriter, _ *http.Request) { + // A progress bar's worth of output with every answer: a write that + // failed would end this process, as it fails ComfyUI's workflow. + if _, err := os.Stdout.Write([]byte(strings.Repeat("progress ", 20000) + "\n")); err != nil { + os.Exit(4) + } + _, _ = io.WriteString(w, "{}") + }) http.HandleFunc("/quit", func(w http.ResponseWriter, _ *http.Request) { os.Exit(3) }) if os.Getenv("LLMGW_FAKE_COMFYUI_EXIT") == "1" { time.AfterFunc(200*time.Millisecond, func() { os.Exit(3) }) @@ -202,3 +210,43 @@ func TestComfyUIExitingEndsTheGate(t *testing.T) { t.Fatalf("err = %v", err) } } + +type brokenWriter struct{} + +func (brokenWriter) Write([]byte) (int, error) { return 0, errors.New("broken pipe") } + +func TestComfyUIKeepsWritingWhenWhatTheGateWritesToIsGone(t *testing.T) { + t.Setenv("LLMGW_FAKE_COMFYUI", "1") + weights := filepath.Join(t.TempDir(), "w.safetensors") + _ = os.WriteFile(weights, []byte("w"), 0o644) + l, _ := net.Listen("tcp", "127.0.0.1:0") + listen := l.Addr().String() + l.Close() + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + done := make(chan error, 1) + go func() { + done <- RunGate(ctx, GateConfig{Listen: listen, Weights: weights, State: t.TempDir(), + Python: os.Args[0], Dir: t.TempDir(), Output: brokenWriter{}}) + }() + ok := 0 + deadline := time.Now().Add(10 * time.Second) + for ok < 20 && time.Now().Before(deadline) { + resp, err := http.Get("http://" + listen + "/health") + if err == nil { + if resp.StatusCode == http.StatusOK { + ok++ + } + resp.Body.Close() + } + select { + case err := <-done: + t.Fatalf("the gate ended: %v", err) + default: + } + time.Sleep(10 * time.Millisecond) + } + if ok < 20 { + t.Fatalf("ComfyUI answered %d times; output it could not hand on stopped it", ok) + } +}