mattn / go-tty

MIT License
208 stars 18 forks source link

fix panic: send on closed channel on windows #25

Closed azr closed 5 years ago

azr commented 5 years ago

if the term is resized when ReadString is being called and no one is reading form ws, we get a panic: send on closed channel.

It could make sense to poll getconsolescreenbufferinfo calls instead of this:

something started from the sigwinch() func

mattn commented 5 years ago

I don't understand what you want to fix (as far as I can see your change of code)

azr commented 5 years ago

Hey @mattn, the main issue is that go-tty tries to send a WINSIZE on a chan that is never read upon and with blocking sends. When you leave the program, go panics.

here's a sample crashing code, just start it and resize the windows.

package main

import (
    "log"

    "github.com/mattn/go-tty"
)

func main() {
    tty, err := tty.Open()
    if err != nil {
        log.Fatal(err)
    }

    log.Print("now, resize the windows, type things and leave:")
    str, err := tty.ReadString()
    log.Printf("str: %s, err: %v", str, err)
    tty.Close()
}

I could repro on windows and mac ( unix ).

On unix I just fixed it by making sure the chans are close in order.

On windows by just using non blocking sends.

Like the golang signal pkg does.

mattn commented 5 years ago

I prefer to use context.Context instead of closeC.

mattn commented 5 years ago

Looks good to me. Thank you.