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 race conditions as mentioned in #148 #169

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
16 changes: 16 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 @@ -113,6 +114,9 @@ type PromptTemplates struct {
// Run will keep the prompt alive until it has been canceled from the command prompt or it has received a valid
// value. It will return the value and an error if any occurred during the prompt's execution.
func (p *Prompt) Run() (string, error) {
mutex := new(sync.Mutex)
closing := false

var err error

err = p.prepareTemplates()
Expand Down Expand Up @@ -159,6 +163,13 @@ func (p *Prompt) Run() (string, error) {
cur := NewCursor(input, p.Pointer, eraseDefault)

listen := func(input []rune, pos int, key rune) ([]rune, int, bool) {
mutex.Lock()
defer mutex.Unlock()

if closing {
return nil, 0, false
}

_, _, keepOn := cur.Listen(input, pos, key)
err := validFn(cur.Get())
var prompt []byte
Expand Down Expand Up @@ -193,6 +204,7 @@ func (p *Prompt) Run() (string, error) {

for {
_, err = rl.Readline()
mutex.Lock()
inputErr = validFn(cur.Get())
if inputErr == nil {
break
Expand All @@ -201,8 +213,12 @@ func (p *Prompt) Run() (string, error) {
if err != nil {
break
}
mutex.Unlock()
}

defer mutex.Unlock()
closing = true

if err != nil {
switch err {
case readline.ErrInterrupt:
Expand Down
15 changes: 15 additions & 0 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"sync"
"text/tabwriter"
"text/template"

Expand Down Expand Up @@ -220,6 +221,9 @@ func (s *Select) RunCursorAt(cursorPos, scroll int) (int, string, error) {
}

func (s *Select) innerRun(cursorPos, scroll int, top rune) (int, string, error) {
mutex := new(sync.Mutex)
closing := false

c := &readline.Config{
Stdin: s.Stdin,
Stdout: s.Stdout,
Expand Down Expand Up @@ -254,6 +258,13 @@ 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) {
mutex.Lock()
defer mutex.Unlock()

if closing {
return nil, 0, false
}

switch {
case key == KeyEnter:
return nil, 0, true
Expand Down Expand Up @@ -373,6 +384,10 @@ func (s *Select) innerRun(cursorPos, scroll int, top rune) (int, string, error)

}

mutex.Lock()
defer mutex.Unlock()
closing = true

if err != nil {
if err.Error() == "Interrupt" {
err = ErrInterrupt
Expand Down