diff --git a/src/unix/tty.c b/src/unix/tty.c index b1782df9..9038d85a 100644 --- a/src/unix/tty.c +++ b/src/unix/tty.c @@ -237,8 +237,10 @@ uv_handle_type uv_guess_handle(uv_file file) { * critical section when the signal was raised. */ int uv_tty_reset_mode(void) { + int saved_errno; int err; + saved_errno = errno; if (!uv_spinlock_trylock(&termios_spinlock)) return -EBUSY; /* In uv_tty_set_mode(). */ @@ -248,5 +250,7 @@ int uv_tty_reset_mode(void) { err = -errno; uv_spinlock_unlock(&termios_spinlock); + errno = saved_errno; + return err; } diff --git a/test/test-tty.c b/test/test-tty.c index cfcb7702..cb742fe1 100644 --- a/test/test-tty.c +++ b/test/test-tty.c @@ -118,6 +118,13 @@ TEST_IMPL(tty) { r = uv_tty_set_mode(&tty_in, UV_TTY_MODE_NORMAL); ASSERT(r == 0); + /* Calling uv_tty_reset_mode() repeatedly should not clobber errno. */ + errno = 0; + ASSERT(0 == uv_tty_reset_mode()); + ASSERT(0 == uv_tty_reset_mode()); + ASSERT(0 == uv_tty_reset_mode()); + ASSERT(0 == errno); + /* TODO check the actual mode! */ uv_close((uv_handle_t*) &tty_in, NULL);