Merge pull request #15 from brettbuddin/read-buffer-race

Fix data race in buffer read
This commit is contained in:
Masashi SHIBATA 2017-10-08 20:53:20 +09:00 committed by GitHub
commit faffcdaa35

@ -12,6 +12,8 @@ import (
const ( const (
logfile = "/tmp/go-prompt-debug.log" logfile = "/tmp/go-prompt-debug.log"
envEnableLog = "GO_PROMPT_ENABLE_LOG" envEnableLog = "GO_PROMPT_ENABLE_LOG"
maxReadBytes = 1024
) )
// Executor is called when user input something text. // Executor is called when user input something text.
@ -249,7 +251,7 @@ func (p *Prompt) tearDown() {
} }
func readBuffer(bufCh chan []byte, stopCh chan struct{}) { func readBuffer(bufCh chan []byte, stopCh chan struct{}) {
buf := make([]byte, 1024) buf := make([]byte, maxReadBytes)
log.Printf("[INFO] readBuffer start") log.Printf("[INFO] readBuffer start")
for { for {
@ -260,7 +262,9 @@ func readBuffer(bufCh chan []byte, stopCh chan struct{}) {
return return
default: default:
if n, err := syscall.Read(syscall.Stdin, buf); err == nil { if n, err := syscall.Read(syscall.Stdin, buf); err == nil {
bufCh <- buf[:n] cbuf := make([]byte, n)
copy(cbuf, buf[:n])
bufCh <- cbuf
} }
} }
} }
@ -278,7 +282,7 @@ func handleSignals(in ConsoleParser, exitCh chan int, winSizeCh chan *WinSize, s
for { for {
select { select {
case <- stop: case <-stop:
log.Println("[INFO] stop handleSignals") log.Println("[INFO] stop handleSignals")
return return
case s := <-sigCh: case s := <-sigCh: