Skip to content
Myr

fix: windows: handle cancel io error

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This gracefully handles windows.CancelIo errors.

authored by Ayman Bagabas · committed by Ayman Bagabas · parent 771a101 · Browse files · 2 files+19 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Fixes: c8d6005325d3 (fix: use CancelIoEx to cancel Windows conInputReader across threads (#1305))
  • Fixes: https://github.com/charmbracelet/bubbletea/issues/1324

Diff stats

2 files+19 added−6 removed

  • other:+19 added−6 removed

2 files+19 added−6 removed

modified:inputreader_windows.go+12 added-2 removed
71 unmodified lines
72 return nil73}7475// Read implements cancelreader.CancelReader.76func (*conInputReader) Read(_ []byte) (n int, err error) {77 return 0, nil76func (r *conInputReader) Read(_ []byte) (n int, err error) {77 if r.isCanceled() {78 err = cancelreader.ErrCanceled79 }80 return81}8283func prepareConsole(input windows.Handle, modes ...uint32) (originalMode uint32, err error) {84 err = windows.GetConsoleMode(input, &originalMode)
22 unmodified lines
107 defer c.lock.Unlock()108109 c.unsafeCanceled = true110}111112func (c *cancelMixin) isCanceled() bool {113 c.lock.Lock()114 defer c.lock.Unlock()115116 return c.unsafeCanceled117}
modified:key_windows.go+7 added-4 removed
8 unmodified lines
9 "io"1011 "github.com/erikgeiser/coninput"12 localereader "github.com/mattn/go-localereader"13 "golang.org/x/sys/windows"13 "github.com/muesli/cancelreader"14)1516func readInputs(ctx context.Context, msgs chan<- Msg, input io.Reader) error {17 if coninReader, ok := input.(*conInputReader); ok {18 return readConInputs(ctx, msgs, coninReader.conin)18 return readConInputs(ctx, msgs, coninReader)19 }2021 return readAnsiInputs(ctx, msgs, localereader.NewReader(input))22}2324func readConInputs(ctx context.Context, msgsch chan<- Msg, con windows.Handle) error {24func readConInputs(ctx context.Context, msgsch chan<- Msg, con *conInputReader) error {25 var ps coninput.ButtonState // keep track of previous mouse state26 var ws coninput.WindowBufferSizeEventRecord // keep track of the last window size event27 for {28 events, err := coninput.ReadNConsoleInputs(con, 16)28 events, err := coninput.ReadNConsoleInputs(con.conin, 16)29 if err != nil {30 if con.isCanceled() {31 return cancelreader.ErrCanceled32 }33 return fmt.Errorf("read coninput events: %w", err)34 }3536 for _, event := range events {
324 unmodified lines