From 7a34cdbaa67a484e887beb95344f801de1f4f6e9 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Sat, 10 Oct 2026 18:37:52 +0200 Subject: [PATCH] fix(up): stop the menu loop when the keyboard channel is closed keyboard.Close closes the channel handed out by GetKeys. Run kept reading zero-value events from it until its context was done. Signed-off-by: Nicolas De Loof --- cmd/formatter/shortcut.go | 5 ++++- cmd/formatter/shortcut_test.go | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/cmd/formatter/shortcut.go b/cmd/formatter/shortcut.go index a098d3866..d6add09c2 100644 --- a/cmd/formatter/shortcut.go +++ b/cmd/formatter/shortcut.go @@ -327,7 +327,10 @@ func (lk *LogKeyboard) Run(ctx context.Context, project *types.Project, options select { case <-ctx.Done(): return - case event := <-lk.keys: + case event, ok := <-lk.keys: + if !ok { // the keyboard was closed + return + } lk.handleKeyEvent(ctx, event, project, options) } } diff --git a/cmd/formatter/shortcut_test.go b/cmd/formatter/shortcut_test.go index 0f85be074..ea76ae953 100644 --- a/cmd/formatter/shortcut_test.go +++ b/cmd/formatter/shortcut_test.go @@ -60,6 +60,25 @@ func TestRunHandlesKeysUntilContextIsDone(t *testing.T) { assert.NilError(t, lk.Close(), "Close must be idempotent") } +func TestRunReturnsWhenKeyboardIsClosed(t *testing.T) { + keys := make(chan keyboard.KeyEvent) + lk := NewKeyboardManager(false, false, make(chan os.Signal, 1)) + lk.keys = keys + + done := make(chan struct{}) + go func() { + lk.Run(t.Context(), &types.Project{}, api.UpOptions{}) + close(done) + }() + + close(keys) // what keyboard.Close does to the channel it handed out + select { + case <-done: + case <-time.After(5 * time.Second): + t.Fatal("Run must return once the keyboard channel is closed") + } +} + func TestRunWithoutOpenReturnsImmediately(t *testing.T) { lk := NewKeyboardManager(false, false, make(chan os.Signal, 1)) done := make(chan struct{})