From 4a8c31c50eb4098122e62ce7b526b40bd52b3a0a Mon Sep 17 00:00:00 2001 From: Brett Buddin Date: Fri, 29 Sep 2017 21:56:33 -0400 Subject: [PATCH] Fixes #12. Makes a copy of the bytes we read from stdin and sends those to the processing goroutine instead of a slice from our scratch buffer. This avoids sharing the same underlying array data across goroutines. --- prompt.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/prompt.go b/prompt.go index 9db1f29..4eb4cf9 100644 --- a/prompt.go +++ b/prompt.go @@ -12,6 +12,8 @@ import ( const ( logfile = "/tmp/go-prompt-debug.log" envEnableLog = "GO_PROMPT_ENABLE_LOG" + + maxReadBytes = 1024 ) // Executor is called when user input something text. @@ -249,7 +251,7 @@ func (p *Prompt) tearDown() { } func readBuffer(bufCh chan []byte, stopCh chan struct{}) { - buf := make([]byte, 1024) + buf := make([]byte, maxReadBytes) log.Printf("[INFO] readBuffer start") for { @@ -260,7 +262,9 @@ func readBuffer(bufCh chan []byte, stopCh chan struct{}) { return default: 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 { select { - case <- stop: + case <-stop: log.Println("[INFO] stop handleSignals") return case s := <-sigCh: