From 0fcb055246d767ea1b8fcdc94e31ffd4c679202c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 15 Jan 2024 20:08:52 +0530 Subject: [PATCH] tty: retry on temporary read errors --- tools/tty/tty.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tools/tty/tty.go b/tools/tty/tty.go index e3d4d4f71..960eeba93 100644 --- a/tools/tty/tty.go +++ b/tools/tty/tty.go @@ -285,8 +285,19 @@ func (self *Term) ReadWithTimeout(b []byte, d time.Duration) (n int, err error) } } -func (self *Term) Read(b []byte) (int, error) { - return self.os_file.Read(b) +func is_temporary_read_error(err error) bool { + return errors.Is(err, unix.EINTR) || errors.Is(err, unix.EAGAIN) || errors.Is(err, unix.EWOULDBLOCK) +} + +func (self *Term) Read(b []byte) (n int, err error) { + for { + n, err = self.os_file.Read(b) + // On macOS we get EAGAIN if another thread is writing to the tty at the same time + if err != nil && is_temporary_read_error(err) && n <= 0 { + continue + } + return + } } func (self *Term) Write(b []byte) (int, error) {