manifoldco / promptui

Interactive prompt for command-line applications
https://www.manifold.co
BSD 3-Clause "New" or "Revised" License
6.02k stars 332 forks source link

Ctrl-C causes panic #43

Open v8oss opened 6 years ago

v8oss commented 6 years ago

I'm getting random panics if I press ctrl-C instead of answering, it's a simple prompt.

I'm checking for promptui.ErrInterrupt but it happens before that inside Run.

        prompt := promptui.Prompt{
            Label:    "Enter the current date in UTC in the format YYYY-MM-DD ",
        }

        _, err := prompt.Run()

        if err == promptui.ErrInterrupt {
            os.Exit(-1)
        }

trace

? Is this the current time: 21:07:03 UTC? [y/N] █
panic: bytes.Buffer: truncation out of range

goroutine 42 [running]:
bytes.(*Buffer).Truncate(0xc4200cc4d0, 0x0)
    /home/stuart/go/src/bytes/buffer.go:74 +0xc1
bytes.(*Buffer).WriteTo(0xc4200cc4d0, 0x7599e0, 0xc42012e260, 0x467754, 0xc4200cc4d0, 0x0)
    /home/stuart/go/src/bytes/buffer.go:228 +0x5b
github.com/manifoldco/promptui/screenbuf.(*ScreenBuf).Flush(0xc42012ce10, 0xc420138240, 0x50)
    /home/stuart/gocode/src/github.com/manifoldco/promptui/screenbuf/screenbuf.go:115 +0x107
github.com/manifoldco/promptui.(*Prompt).Run.func2(0x78e658, 0x0, 0x0, 0x0, 0xc400000003, 0x5812d7, 0xc42014c090, 0x6629a0, 0xc42014c090, 0xc42014c090)
    /home/stuart/gocode/src/github.com/manifoldco/promptui/prompt.go:179 +0x2d8
github.com/chzyer/readline.(*DumpListener).OnChange(0xc420136058, 0x78e658, 0x0, 0x0, 0x0, 0x3, 0x3, 0x0, 0x0, 0x0, ...)
    /home/stuart/gocode/src/github.com/chzyer/readline/operation.go:516 +0x63
github.com/chzyer/readline.(*Operation).ioloop(0xc4200cc380)
    /home/stuart/gocode/src/github.com/chzyer/readline/operation.go:339 +0x405
created by github.com/chzyer/readline.NewOperation
    /home/stuart/gocode/src/github.com/chzyer/readline/operation.go:88 +0x3bc
louisbranch commented 6 years ago

@v8oss can you post a full snippet/gist of the function? Your code shows a default prompt, but the panic seems to be a confirmation prompt.

I tried replicating the problem with both prompt and confirm prompts, seems to be working ok. Are you running it inside a go-routine?

shawn111 commented 6 years ago

@luizbranco I tried to run promptui/_examples/confirm/main.go, I can find this problem about 30% chance. Crtl+C or Ctrl+D

[shawn@se129 confirm]$ ./main ? Delete Resource? [y/N] █

panic: bytes.Buffer: truncation out of range

goroutine 1 [running]: bytes.(Buffer).Truncate(0xc4200b2070, 0x0) /usr/lib/golang/src/bytes/buffer.go:74 +0xc1 bytes.(Buffer).WriteTo(0xc4200b2070, 0x657700, 0xc4200da000, 0x68a4c8, 0x0, 0x0) /usr/lib/golang/src/bytes/buffer.go:228 +0x5b github.com/manifoldco/promptui/screenbuf.(ScreenBuf).Flush(0xc4200e0000, 0x0, 0x0) /home/users/shawn/go/src/github.com/manifoldco/promptui/screenbuf/screenbuf.go:115 +0x107 github.com/manifoldco/promptui.(Prompt).Run(0xc42005d2d0, 0xc420051f00, 0x564080, 0xc420010cb0, 0xc42000c500) /home/users/shawn/go/src/github.com/manifoldco/promptui/prompt.go:220 +0x9c9 main.main() /home/users/shawn/go/src/github.com/manifoldco/promptui/_examples/confirm/main.go:15 +0x111

shawn111 commented 6 years ago

Hi @luizbranco, This issue is related on go version.

go version go1.10 linux/amd64 (ubuntu 18.04) good go version go1.8.3 linux/amd64 (centos 7.4) bad go version go1.10.3 linux/amd64 (centos 7.4) good >> https://go-repo.io/

Thanks,