From e9d291086d5303141b0f58f8fcf6c528837cf21d Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sat, 14 Jun 2025 22:15:28 +0900 Subject: [PATCH] Limit the maximum number of background processes --- src/constants.go | 1 + src/terminal.go | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/src/constants.go b/src/constants.go index ececdb97..bed1748e 100644 --- a/src/constants.go +++ b/src/constants.go @@ -28,6 +28,7 @@ const ( previewDelayed = 500 * time.Millisecond maxPatternLength = 1000 maxMulti = math.MaxInt32 + maxBgProcesses = 200 // Matcher numPartitionsMultiplier = 8 diff --git a/src/terminal.go b/src/terminal.go index af8c8fae..e18a125b 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -389,6 +389,7 @@ type Terminal struct { killChan chan bool serverInputChan chan []*action callbackChan chan func() + semaphore chan struct{} keyChan chan tui.Event eventChan chan tui.Event slab *util.Slab @@ -1034,6 +1035,7 @@ func NewTerminal(opts *Options, eventBox *util.EventBox, executor *util.Executor killChan: make(chan bool), serverInputChan: make(chan []*action, 100), callbackChan: make(chan func(), 100), + semaphore: make(chan struct{}, maxBgProcesses), keyChan: make(chan tui.Event), eventChan: make(chan tui.Event, 6), // start | (load + result + zero|one) | (focus) | (resize) tui: renderer, @@ -4342,6 +4344,8 @@ func (t *Terminal) captureAsync(template string, firstLineOnly bool, callback fu _, list := t.buildPlusList(template, false) command, tempFiles := t.replacePlaceholder(template, false, string(t.input), list) go func() { + t.semaphore <- struct{}{} + cmd := t.executor.ExecCommand(command, false) cmd.Env = t.environ() @@ -4360,6 +4364,7 @@ func (t *Terminal) captureAsync(template string, firstLineOnly bool, callback fu } removeFiles(tempFiles) + <-t.semaphore t.callbackChan <- func() { callback(output) } }() }