Skip to content

Commit 57c16e2

Browse files
committed
fix: don't abort on first encountered error when resetting Windows console
When resetting the Windows console modes, we must not abort on the first encountered error, otherwise we might leave the console in an unexpected state. This is easily reproducible by calling SetRaw while stdout is redirected: Reset will abort restoring console modes after failing to reset stdout, which leaves stderr in its raw state. Signed-off-by: Christian Muehlhaeuser <[email protected]>
1 parent f6c071f commit 57c16e2

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

console_windows.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ func (m *master) SetRaw() error {
9494
}
9595

9696
func (m *master) Reset() error {
97+
var errs []error
98+
9799
for _, s := range []struct {
98100
fd windows.Handle
99101
mode uint32
@@ -103,10 +105,16 @@ func (m *master) Reset() error {
103105
{m.err, m.errMode},
104106
} {
105107
if err := windows.SetConsoleMode(s.fd, s.mode); err != nil {
106-
return fmt.Errorf("unable to restore console mode: %w", err)
108+
// we can't just abort on the first error, otherwise we might leave
109+
// the console in an unexpected state.
110+
errs = append(errs, fmt.Errorf("unable to restore console mode: %w", err))
107111
}
108112
}
109113

114+
if len(errs) > 0 {
115+
return errs[0]
116+
}
117+
110118
return nil
111119
}
112120

0 commit comments

Comments
 (0)