Skip to content

Commit 920d07b

Browse files
committed
fix(windows): clean up windows input reader
Remove unused and use CancelIo API to cancel reads.
1 parent ef4e46c commit 920d07b

File tree

1 file changed

+2
-49
lines changed

1 file changed

+2
-49
lines changed

inputreader_windows.go

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,9 @@ import (
1717
type conInputReader struct {
1818
cancelMixin
1919

20-
conin windows.Handle
21-
cancelEvent windows.Handle
20+
conin windows.Handle
2221

2322
originalMode uint32
24-
25-
// inputEvent holds the input event that was read in order to avoid
26-
// unnecessary allocations. This re-use is possible because
27-
// InputRecord.Unwarp which is called inparseInputMsgFromInputRecord
28-
// returns an data structure that is independent of the passed InputRecord.
29-
inputEvent []coninput.InputRecord
3023
}
3124

3225
var _ cancelreader.CancelReader = &conInputReader{}
@@ -53,14 +46,8 @@ func newInputReader(r io.Reader) (cancelreader.CancelReader, error) {
5346
return nil, fmt.Errorf("failed to prepare console input: %w", err)
5447
}
5548

56-
cancelEvent, err := windows.CreateEvent(nil, 0, 0, nil)
57-
if err != nil {
58-
return nil, fmt.Errorf("create stop event: %w", err)
59-
}
60-
6149
return &conInputReader{
6250
conin: conin,
63-
cancelEvent: cancelEvent,
6451
originalMode: originalMode,
6552
}, nil
6653
}
@@ -69,21 +56,11 @@ func newInputReader(r io.Reader) (cancelreader.CancelReader, error) {
6956
func (r *conInputReader) Cancel() bool {
7057
r.setCanceled()
7158

72-
err := windows.SetEvent(r.cancelEvent)
73-
if err != nil {
74-
return false
75-
}
76-
77-
return true
59+
return windows.CancelIo(r.conin) == nil
7860
}
7961

8062
// Close implements cancelreader.CancelReader.
8163
func (r *conInputReader) Close() error {
82-
err := windows.CloseHandle(r.cancelEvent)
83-
if err != nil {
84-
return fmt.Errorf("closing cancel event handle: %w", err)
85-
}
86-
8764
if r.originalMode != 0 {
8865
err := windows.SetConsoleMode(r.conin, r.originalMode)
8966
if err != nil {
@@ -115,30 +92,6 @@ func prepareConsole(input windows.Handle, modes ...uint32) (originalMode uint32,
11592
return originalMode, nil
11693
}
11794

118-
func waitForInput(conin, cancel windows.Handle) error {
119-
event, err := windows.WaitForMultipleObjects([]windows.Handle{conin, cancel}, false, windows.INFINITE)
120-
switch {
121-
case windows.WAIT_OBJECT_0 <= event && event < windows.WAIT_OBJECT_0+2:
122-
if event == windows.WAIT_OBJECT_0+1 {
123-
return cancelreader.ErrCanceled
124-
}
125-
126-
if event == windows.WAIT_OBJECT_0 {
127-
return nil
128-
}
129-
130-
return fmt.Errorf("unexpected wait object is ready: %d", event-windows.WAIT_OBJECT_0)
131-
case windows.WAIT_ABANDONED <= event && event < windows.WAIT_ABANDONED+2:
132-
return fmt.Errorf("abandoned")
133-
case event == uint32(windows.WAIT_TIMEOUT):
134-
return fmt.Errorf("timeout")
135-
case event == windows.WAIT_FAILED:
136-
return fmt.Errorf("failed")
137-
default:
138-
return fmt.Errorf("unexpected error: %w", err)
139-
}
140-
}
141-
14295
// cancelMixin represents a goroutine-safe cancelation status.
14396
type cancelMixin struct {
14497
unsafeCanceled bool

0 commit comments

Comments
 (0)