From 57c16e2312de0817cb59675d0fe625e0e992b67e Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Fri, 21 Oct 2022 18:05:12 +0200 Subject: [PATCH] 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 --- console_windows.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/console_windows.go b/console_windows.go index 787c11f..09aa051 100644 --- a/console_windows.go +++ b/console_windows.go @@ -94,6 +94,8 @@ func (m *master) SetRaw() error { } func (m *master) Reset() error { + var errs []error + for _, s := range []struct { fd windows.Handle mode uint32 @@ -103,10 +105,16 @@ func (m *master) Reset() error { {m.err, m.errMode}, } { if err := windows.SetConsoleMode(s.fd, s.mode); err != nil { - return fmt.Errorf("unable to restore console mode: %w", err) + // we can't just abort on the first error, otherwise we might leave + // the console in an unexpected state. + errs = append(errs, fmt.Errorf("unable to restore console mode: %w", err)) } } + if len(errs) > 0 { + return errs[0] + } + return nil }