protect peco from race

This commit is contained in:
Daisuke Maki 2016-06-23 04:56:09 -04:00
parent a6589820d0
commit 9cbe49c72d
3 changed files with 18 additions and 1 deletions

View file

@ -7,6 +7,7 @@ import (
"time"
"github.com/lestrrat/go-pdebug"
"github.com/peco/peco/internal/util"
"github.com/peco/peco/pipeline"
"github.com/pkg/errors"
"golang.org/x/net/context"
@ -180,6 +181,14 @@ func (s *Source) Setup(state *Peco) {
close(s.ready)
}
scanner := bufio.NewScanner(s.in)
defer func() {
if util.IsTty(s.in) {
return
}
if closer, ok := s.in.(io.Closer); ok {
closer.Close()
}
}()
readCount := 0
for scanner.Scan() {

View file

@ -72,6 +72,7 @@ type Peco struct {
inputseq Inputseq // current key sequence (just the names)
layoutType string
location Location
mutex sync.Mutex
prompt string
query Query
queryExecDelay time.Duration

View file

@ -97,10 +97,14 @@ func (p *Peco) Location() *Location {
}
func (p *Peco) ResultCh() chan Line {
p.mutex.Lock()
defer p.mutex.Unlock()
return p.resultCh
}
func (p *Peco) SetResultCh(ch chan Line) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.resultCh = ch
}
@ -358,7 +362,6 @@ func (p *Peco) SetupSource() (s *Source, err error) {
if err != nil {
return nil, errors.Wrap(err, "failed to open file for input")
}
defer f.Close() // ONLY do this here, you don't want to close Stdin
in = f
case !util.IsTty(p.Stdin):
in = p.Stdin
@ -483,10 +486,14 @@ func (p *Peco) populateStyles() error {
}
func (p *Peco) CurrentLineBuffer() Buffer {
p.mutex.Lock()
defer p.mutex.Unlock()
return p.currentLineBuffer
}
func (p *Peco) SetCurrentLineBuffer(b Buffer) {
p.mutex.Lock()
defer p.mutex.Unlock()
if pdebug.Enabled {
g := pdebug.Marker("Peco.SetCurrentLineBuffer %s", reflect.TypeOf(b).String())
defer g.End()