Skip to content
Myr

Remove possible races (#4070)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Junegunn Choi · committed by Junegunn Choi · parent bacc860 · Browse files · 3 files+39 added−39 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+39 added−39 removed

  • other:+39 added−39 removed

3 files+39 added−39 removed

modified:src/core.go+7 added-3 removed
170 unmodified lines
171 reader = NewReader(func(data []byte) bool {172 return chunkList.Push(data)173 }, eventBox, executor, opts.ReadZero, opts.Filter == nil)174175 readyChan := make(chan bool)175 go reader.ReadSource(opts.Input, opts.WalkerRoot, opts.WalkerOpts, opts.WalkerSkip, initialReload, initialEnv)176 go reader.ReadSource(opts.Input, opts.WalkerRoot, opts.WalkerOpts, opts.WalkerSkip, initialReload, initialEnv, readyChan)177 <-readyChan178 }179180 // Matcher181 forward := true
43 unmodified lines
225 mutex.Unlock()226 }227 return false228 }, eventBox, executor, opts.ReadZero, false)227 reader.ReadSource(opts.Input, opts.WalkerRoot, opts.WalkerOpts, opts.WalkerSkip, initialReload, initialEnv)229 reader.ReadSource(opts.Input, opts.WalkerRoot, opts.WalkerOpts, opts.WalkerSkip, initialReload, initialEnv, nil)230 } else {231 eventBox.Unwatch(EvtReadNew)232 eventBox.WaitFor(EvtReadFin)233
66 unmodified lines
300 chunkList.Clear()301 itemIndex = 0302 inputRevision.bumpMajor()303 header = make([]string, 0, opts.HeaderLines)302 reader.restart(command, environ)304 readyChan := make(chan bool)305 go reader.restart(command, environ, readyChan)306 <-readyChan307 }308309 exitCode := ExitOk310 stop := false
155 unmodified lines
modified:src/reader_test.go+8 added-4 removed
21 unmodified lines
22 t.Error("EvtReadNew should not be set yet")23 }2425 // Normal command26 counter := 027 ready := func() {28 counter++29 }26 reader.fin(reader.readFromCommand(`echo abc&&echo def`, nil))27 if len(strs) != 2 || strs[0] != "abc" || strs[1] != "def" {30 reader.fin(reader.readFromCommand(`echo abc&&echo def`, nil, ready))31 if len(strs) != 2 || strs[0] != "abc" || strs[1] != "def" || counter != 1 {32 t.Errorf("%s", strs)33 }3435 // Check EventBox again
15 unmodified lines
51 // Restart event poller52 reader.startEventPoller()5354 // Failing command51 reader.fin(reader.readFromCommand(`no-such-command`, nil))55 reader.fin(reader.readFromCommand(`no-such-command`, nil, ready))56 strs = []string{}53 if len(strs) > 0 {57 if len(strs) > 0 || counter != 2 {58 t.Errorf("%s", strs)59 }6061 // Check EventBox again
7 unmodified lines
modified:src/reader.go+24 added-32 removed
4 unmodified lines
5 "context"6 "io"7 "io/fs"8 "os"9 "os/exec"9 "path/filepath"10 "sync"11 "sync/atomic"12 "time"
83 unmodified lines
96 }97 r.mutex.Unlock()98}99101func (r *Reader) restart(command commandSpec, environ []string) {100func (r *Reader) restart(command commandSpec, environ []string, readyChan chan bool) {101 r.event = int32(EvtReady)102 r.startEventPoller()104105 r.mutex.Lock()106 defer r.mutex.Unlock()107108 if exec, execOut := r.startCommand(command.command, environ); exec != nil {109 go func() {110 success := r.feedCommandOutput(exec, execOut)111 r.fin(success)112 removeFiles(command.tempFiles)113 }()114 }103 success := r.readFromCommand(command.command, environ, func() {104 readyChan <- true105 })106 r.fin(success)107 removeFiles(command.tempFiles)108}109110func (r *Reader) readChannel(inputChan chan string) bool {111 for {
8 unmodified lines
120 return true121}122123// ReadSource reads data from the default command or from standard input131func (r *Reader) ReadSource(inputChan chan string, root string, opts walkerOpts, ignores []string, initCmd string, initEnv []string) {124func (r *Reader) ReadSource(inputChan chan string, root string, opts walkerOpts, ignores []string, initCmd string, initEnv []string, readyChan chan bool) {125 r.startEventPoller()126 var success bool127 signalReady := func() {128 if readyChan != nil {129 readyChan <- true130 }131 }132 if inputChan != nil {133 signalReady()134 success = r.readChannel(inputChan)135 } else if len(initCmd) > 0 {137 success = r.readFromCommand(initCmd, initEnv)136 success = r.readFromCommand(initCmd, initEnv, signalReady)137 } else if util.IsTty(os.Stdin) {138 cmd := os.Getenv("FZF_DEFAULT_COMMAND")139 if len(cmd) == 0 {140 signalReady()141 success = r.readFiles(root, opts, ignores)142 } else {143 success = r.readFromCommand(cmd, initEnv)143 success = r.readFromCommand(cmd, initEnv, signalReady)144 }145 } else {146 signalReady()147 success = r.readFromStdin()148 }149 r.fin(success)150}
153 unmodified lines
304 }305 return fastwalk.Walk(&conf, root, fn) == nil306}307307// Should be called with the mutex held308func (r *Reader) startCommand(command string, environ []string) (*exec.Cmd, io.ReadCloser) {308func (r *Reader) readFromCommand(command string, environ []string, signalReady func()) bool {309 r.mutex.Lock()310311 r.termFunc = nil312 r.command = &command313 exec := r.executor.ExecCommand(command, true)314 if environ != nil {315 exec.Env = environ316 }317 execOut, err := exec.StdoutPipe()318 if err != nil || exec.Start() != nil {317 return nil, nil319 r.mutex.Unlock()320 return false321 }322323 // Function to call to terminate the running command324 r.termFunc = func() {325 execOut.Close()326 util.KillCommand(exec)327 }328326 return exec, execOut327}329 signalReady()330 r.mutex.Unlock()331329func (r *Reader) feedCommandOutput(exec *exec.Cmd, execOut io.ReadCloser) bool {332 r.feed(execOut)333 return exec.Wait() == nil334}333334func (r *Reader) readFromCommand(command string, environ []string) bool {335 r.mutex.Lock()336 exec, execOut := r.startCommand(command, environ)337 r.mutex.Unlock()338 if exec == nil {339 return false340 }341 return r.feedCommandOutput(exec, execOut)342}