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
Provenance
- Commit
19495eb- Tree
feec000- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
bacc860
3 files changed+39 added−39 removed
modified:src/core.go+7 added-3 removed
170 unmodified lines171 reader = NewReader(func(data []byte) bool {172 return chunkList.Push(data)173 }, eventBox, executor, opts.ReadZero, opts.Filter == nil)174 175+ 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 }179 180 // Matcher181 forward := true43 unmodified lines225 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 lines300 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 }308 309 exitCode := ExitOk310 stop := false155 unmodified linesmodified:src/reader_test.go+8 added-4 removed
21 unmodified lines22 t.Error("EvtReadNew should not be set yet")23 }24 25 // 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 }34 35 // Check EventBox again15 unmodified lines51 // Restart event poller52 reader.startEventPoller()53 54 // 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 }60 61 // Check EventBox again7 unmodified linesmodified:src/reader.go+24 added-32 removed
4 unmodified lines5 "context"6 "io"7 "io/fs"8 "os"9- "os/exec"9 "path/filepath"10 "sync"11 "sync/atomic"12 "time"83 unmodified lines96 }97 r.mutex.Unlock()98 }99 101-func (r *Reader) restart(command commandSpec, environ []string) {100+func (r *Reader) restart(command commandSpec, environ []string, readyChan chan bool) {101 r.event = int32(EvtReady)102 r.startEventPoller()104-105- r.mutex.Lock()106- defer r.mutex.Unlock()107-108- 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 }109 110 func (r *Reader) readChannel(inputChan chan string) bool {111 for {8 unmodified lines120 return true121 }122 123 // ReadSource reads data from the default command or from standard input131-func (r *Reader) ReadSource(inputChan chan string, root string, opts walkerOpts, ignores []string, initCmd string, initEnv []string) {124+func (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 lines304 }305 return fastwalk.Walk(&conf, root, fn) == nil306 }307 307-// Should be called with the mutex held308-func (r *Reader) startCommand(command string, environ []string) (*exec.Cmd, io.ReadCloser) {308+func (r *Reader) readFromCommand(command string, environ []string, signalReady func()) bool {309+ r.mutex.Lock()310+311 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 }322 323 // Function to call to terminate the running command324 r.termFunc = func() {325 execOut.Close()326 util.KillCommand(exec)327 }328 326- return exec, execOut327-}329+ signalReady()330+ r.mutex.Unlock()331 329-func (r *Reader) feedCommandOutput(exec *exec.Cmd, execOut io.ReadCloser) bool {332 r.feed(execOut)333 return exec.Wait() == nil334 }333-334-func (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-}