diff --git a/shellterm.go b/shellterm.go index a85cbf6..f6e8db7 100644 --- a/shellterm.go +++ b/shellterm.go @@ -242,7 +242,13 @@ func (t *Session) key(k lineedit.Key) { } return } - ctx, cancel := context.WithTimeout(context.Background(), CompleteTimeout) + // Only Tab asks the completer, so only Tab needs a deadline: a context and + // a timer per keystroke cost four allocations and a runtime timer for + // nothing (wasmdesk/wasmbox#175). + ctx, cancel := context.Background(), context.CancelFunc(func() {}) + if k.Code == lineedit.KeyTab { + ctx, cancel = context.WithTimeout(ctx, CompleteTimeout) + } before := t.ed.State() ev := t.ed.Key(ctx, k) cancel() diff --git a/shellterm_test.go b/shellterm_test.go index 7091147..144465d 100644 --- a/shellterm_test.go +++ b/shellterm_test.go @@ -310,3 +310,28 @@ func TestSessionPromptHook(t *testing.T) { r.ses.Input("z\r") r.until("after SetShell", func() bool { return !r.running() && r.line() == "swapped $" }) } + +// A completion runs under CompleteTimeout; the keys that do not complete get no +// timer (wasmdesk/wasmbox#175). +func TestTabCompletionHasADeadline(t *testing.T) { + r := newRig(t, Options{}) + var mu sync.Mutex + var deadlines []bool + r.ses.SetShell(&recShell{}, func(ctx context.Context, _ []string, _ string) ([]lineedit.Candidate, bool) { + _, ok := ctx.Deadline() + mu.Lock() + deadlines = append(deadlines, ok) + mu.Unlock() + return nil, false + }) + r.attach() + r.ses.Input("a") + r.ses.Input("\t") + r.settle() + r.until("the completer ran", func() bool { mu.Lock(); defer mu.Unlock(); return len(deadlines) == 1 }) + mu.Lock() + defer mu.Unlock() + if !deadlines[0] { + t.Error("Tab's context has no deadline") + } +}