diff --git a/core/engine.go b/core/engine.go index 4a37f25..f18d2a5 100644 --- a/core/engine.go +++ b/core/engine.go @@ -4423,7 +4423,11 @@ func drainEvents(ch <-chan Event) { drained := 0 for { select { - case <-ch: + case _, ok := <-ch: + if !ok { + // Channel is closed; stop immediately to avoid an infinite loop. + return + } drained++ default: if drained > 0 { diff --git a/core/engine_test.go b/core/engine_test.go index f394677..d36b05a 100644 --- a/core/engine_test.go +++ b/core/engine_test.go @@ -2726,3 +2726,49 @@ func TestCmdBindSetup_UsesSharedLogic(t *testing.T) { t.Error("expected instructions written to file") } } + +func TestDrainEventsClosedChannel(t *testing.T) { + ch := make(chan Event, 2) + ch <- Event{Type: EventToolUse, Content: "a"} + ch <- Event{Type: EventToolUse, Content: "b"} + close(ch) + + done := make(chan struct{}) + go func() { + drainEvents(ch) + close(done) + }() + + select { + case <-done: + // ok — returned promptly + case <-time.After(2 * time.Second): + t.Fatal("drainEvents did not return on closed channel (infinite loop)") + } +} + +func TestDrainEventsOpenChannel(t *testing.T) { + ch := make(chan Event, 3) + ch <- Event{Type: EventToolUse, Content: "a"} + ch <- Event{Type: EventToolUse, Content: "b"} + + done := make(chan struct{}) + go func() { + drainEvents(ch) + close(done) + }() + + select { + case <-done: + // ok + case <-time.After(2 * time.Second): + t.Fatal("drainEvents did not return on open channel with buffered events") + } + + // Channel should now be empty. + select { + case <-ch: + t.Fatal("expected channel to be drained") + default: + } +}