Terminate running background transform on exit
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Close #4422
authored by Junegunn Choi · committed by Junegunn Choi · parent b2a8a28 · Browse files · 2 files·+47 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+47 added−0 removed
- other:+47 added−0 removed
Provenance
- Commit
247d168- Tree
75faf76- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
b2a8a28
2 files changed+47 added−0 removed
added:src/util/concurrent_set.go+39 added
1+package util2+3+import "sync"4+5+// ConcurrentSet is a thread-safe set implementation.6+type ConcurrentSet[T comparable] struct {7+ lock sync.RWMutex8+ items map[T]struct{}9+}10+11+// NewConcurrentSet creates a new ConcurrentSet.12+func NewConcurrentSet[T comparable]() *ConcurrentSet[T] {13+ return &ConcurrentSet[T]{14+ items: make(map[T]struct{}),15+ }16+}17+18+// Add adds an item to the set.19+func (s *ConcurrentSet[T]) Add(item T) {20+ s.lock.Lock()21+ defer s.lock.Unlock()22+ s.items[item] = struct{}{}23+}24+25+// Remove removes an item from the set.26+func (s *ConcurrentSet[T]) Remove(item T) {27+ s.lock.Lock()28+ defer s.lock.Unlock()29+ delete(s.items, item)30+}31+32+// ForEach iterates over each item in the set and applies the provided function.33+func (s *ConcurrentSet[T]) ForEach(fn func(item T)) {34+ s.lock.RLock()35+ defer s.lock.RUnlock()36+ for item := range s.items {37+ fn(item)38+ }39+}modified:src/terminal.go+8 added
7 unmodified lines8 "io"9 "math"10 "net"11 "os"12+ "os/exec"13 "os/signal"14 "regexp"15 "sort"16 "strconv"360 unmodified lines377 selected map[int32]selectedItem378 version int64379 revision revision380 bgVersion int64381+ runningCmds *util.ConcurrentSet[*exec.Cmd]382 reqBox *util.EventBox383 initialPreviewOpts previewOpts384 previewOpts previewOpts385 activePreviewOpts *previewOpts645 unmodified lines1031 printsep: opts.PrintSep,1032 proxyScript: opts.ProxyScript,1033 merger: EmptyMerger(revision{}),1034 selected: make(map[int32]selectedItem),1035+ runningCmds: util.NewConcurrentSet[*exec.Cmd](),1036 reqBox: util.NewEventBox(),1037 initialPreviewOpts: opts.Preview,1038 previewOpts: opts.Preview,1039 activePreviewOpts: &opts.Preview,3339 unmodified lines4379 out, _ := cmd.StdoutPipe()4380 reader := bufio.NewReader(out)4381 var output string4382 if err := cmd.Start(); err == nil {4383+ t.runningCmds.Add(cmd)4384 if firstLineOnly {4385 output, _ = reader.ReadString('\n')4386 output = strings.TrimRight(output, "\r\n")4387 } else {4388 bytes, _ := io.ReadAll(reader)4389 output = string(bytes)4390 }4391 cmd.Wait()4392+ t.runningCmds.Remove(cmd)4393 }4394 t.callbackChan <- versionedCallback{version, func() { callback(output) }}4395 }4396 removeFiles(tempFiles)660 unmodified lines5057 code = getCode()5058 if code <= ExitNoMatch && t.history != nil {5059 t.history.append(string(t.input))5060 }5061+ t.runningCmds.ForEach(func(cmd *exec.Cmd) {5062+ util.KillCommand(cmd)5063+ })5064 running = false5065 t.mutex.Unlock()5066 }5067 1822 unmodified lines