Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix data race in prompt and select. #185

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"strings"
"sync"
"text/template"

"github.com/chzyer/readline"
Expand Down Expand Up @@ -157,10 +158,14 @@ func (p *Prompt) Run() (string, error) {
}
eraseDefault := input != "" && !p.AllowEdit
cur := NewCursor(input, p.Pointer, eraseDefault)
var curLock sync.Mutex

listen := func(input []rune, pos int, key rune) ([]rune, int, bool) {
defer curLock.Unlock()
curLock.Lock()
_, _, keepOn := cur.Listen(input, pos, key)
err := validFn(cur.Get())

var prompt []byte

if err != nil {
Expand Down Expand Up @@ -193,7 +198,10 @@ func (p *Prompt) Run() (string, error) {

for {
_, err = rl.Readline()
curLock.Lock()
inputErr = validFn(cur.Get())
curLock.Unlock()

if inputErr == nil {
break
}
Expand All @@ -203,6 +211,8 @@ func (p *Prompt) Run() (string, error) {
}
}

defer curLock.Unlock()
curLock.Lock()
if err != nil {
switch err {
case readline.ErrInterrupt:
Expand Down
7 changes: 6 additions & 1 deletion select.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"sync"
"text/template"

"github.com/chzyer/readline"
Expand Down Expand Up @@ -246,6 +247,7 @@ func (s *Select) innerRun(cursorPos, scroll int, top rune) (int, string, error)
rl.Write([]byte(hideCursor))
sb := screenbuf.New(rl)

var curLock sync.Mutex
cur := NewCursor("", s.Pointer, false)

canSearch := s.Searcher != nil
Expand All @@ -254,6 +256,8 @@ func (s *Select) innerRun(cursorPos, scroll int, top rune) (int, string, error)
s.list.SetStart(scroll)

c.SetListener(func(line []rune, pos int, key rune) ([]rune, int, bool) {
defer curLock.Unlock()
curLock.Lock()
switch {
case key == KeyEnter:
return nil, 0, true
Expand Down Expand Up @@ -372,7 +376,8 @@ func (s *Select) innerRun(cursorPos, scroll int, top rune) (int, string, error)
}

}

defer curLock.Unlock()
curLock.Lock()
if err != nil {
if err.Error() == "Interrupt" {
err = ErrInterrupt
Expand Down