Skip to content
Myr

Fix race in reload action

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

Fix #4070

authored by Junegunn Choi · committed by Junegunn Choi · parent 82ebcd9 · Browse files · 2 files+60 added−36 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+60 added−36 removed

  • other:+60 added−36 removed

2 files+60 added−36 removed

modified:src/core.go+1 added-1 removed
297 unmodified lines
298 chunkList.Clear()299 itemIndex = 0300 inputRevision.bumpMajor()301 header = make([]string, 0, opts.HeaderLines)302 go reader.restart(command, environ)302 reader.restart(command, environ)303 }304305 exitCode := ExitOk306 stop := false
155 unmodified lines
modified:src/reader.go+59 added-35 removed
23 unmodified lines
24 delimNil bool25 event int3226 finChan chan bool27 mutex sync.Mutex28 exec *exec.Cmd29 execOut io.ReadCloser28 termFunc func()29 command *string31 killed bool30 wait bool31}3233// NewReader returns new Reader object34func NewReader(pusher func([]byte) bool, eventBox *util.EventBox, executor *util.Executor, delimNil bool, wait bool) *Reader {37 return &Reader{pusher, executor, eventBox, delimNil, int32(EvtReady), make(chan bool, 1), sync.Mutex{}, nil, nil, nil, false, wait}35 return &Reader{36 pusher,37 executor,38 eventBox,39 delimNil,40 int32(EvtReady),41 make(chan bool, 1),42 sync.Mutex{},43 func() { os.Stdin.Close() },44 nil,45 wait}46}4748func (r *Reader) startEventPoller() {49 go func() {
18 unmodified lines
68 }69 }()70}7172func (r *Reader) wasKilled() bool {73 return r.termFunc == nil74}7576func (r *Reader) fin(success bool) {77 atomic.StoreInt32(&r.event, int32(EvtReadFin))78 if r.wait {79 <-r.finChan80 }8182 r.mutex.Lock()83 ret := r.command72 if success || r.killed {84 if success || r.wasKilled() {85 ret = nil86 }87 r.mutex.Unlock()88
1 unmodified line
90}9192func (r *Reader) terminate() {93 r.mutex.Lock()82 r.killed = true83 if r.exec != nil && r.exec.Process != nil {84 r.execOut.Close()85 util.KillCommand(r.exec)86 } else {87 os.Stdin.Close()94 if r.termFunc != nil {95 r.termFunc()96 r.termFunc = nil97 }98 r.mutex.Unlock()99}100101func (r *Reader) restart(command commandSpec, environ []string) {102 r.event = int32(EvtReady)103 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() {95 success := r.readFromCommand(command.command, environ)96 r.fin(success)97 removeFiles(command.tempFiles)110 success := r.feedCommandOutput(exec, execOut)111 r.fin(success)112 removeFiles(command.tempFiles)113 }()114 }115}116117func (r *Reader) readChannel(inputChan chan string) bool {118 for {
146 unmodified lines
265 return byteString(bytes)266}267268func (r *Reader) readFiles(root string, opts walkerOpts, ignores []string) bool {252 r.killed = false269 conf := fastwalk.Config{270 Follow: opts.follow,271 // Use forward slashes when running a Windows binary under WSL or MSYS272 ToSlash: fastwalk.DefaultToSlash(),
22 unmodified lines
295 }296 }297 r.mutex.Lock()298 defer r.mutex.Unlock()283 if r.killed {299 if r.wasKilled() {300 return context.Canceled301 }302 return nil303 }304 return fastwalk.Walk(&conf, root, fn) == nil305}306291func (r *Reader) readFromCommand(command string, environ []string) bool {292 r.mutex.Lock()293 r.killed = false307// Should be called with the mutex held308func (r *Reader) startCommand(command string, environ []string) (*exec.Cmd, io.ReadCloser) {309 r.termFunc = nil310 r.command = &command295 r.exec = r.executor.ExecCommand(command, true)311 exec := r.executor.ExecCommand(command, true)312 if environ != nil {297 r.exec.Env = environ313 exec.Env = environ314 }299300 var err error301 r.execOut, err = r.exec.StdoutPipe()302 if err != nil {303 r.exec = nil304 r.mutex.Unlock()305 return false315 execOut, err := exec.StdoutPipe()316 if err != nil || exec.Start() != nil {317 return nil, nil318 }319308 err = r.exec.Start()309 if err != nil {310 r.exec = nil311 r.mutex.Unlock()312 return false320 // Function to call to terminate the running command321 r.termFunc = func() {322 execOut.Close()323 util.KillCommand(exec)324 }325326 return exec, execOut327}328329func (r *Reader) feedCommandOutput(exec *exec.Cmd, execOut io.ReadCloser) bool {330 r.feed(execOut)331 return exec.Wait() == nil332}333334func (r *Reader) readFromCommand(command string, environ []string) bool {335 r.mutex.Lock()336 exec, execOut := r.startCommand(command, environ)337 r.mutex.Unlock()316 r.feed(r.execOut)317 return r.exec.Wait() == nil338 if exec == nil {339 return false340 }341 return r.feedCommandOutput(exec, execOut)342}