From 7dd6e6c8c195ddd89b1c90646126f87c9c9ea597 Mon Sep 17 00:00:00 2001 From: Michael Hines Date: Fri, 29 Nov 2024 07:14:29 -0500 Subject: [PATCH] GUI h.quit() leaves terminal in raw state under thread sanitizer. system("stty sane") errcode is 139. --- src/nrnpython/inithoc.cpp | 42 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/src/nrnpython/inithoc.cpp b/src/nrnpython/inithoc.cpp index 9e9c9ef508..f5ee0043a9 100644 --- a/src/nrnpython/inithoc.cpp +++ b/src/nrnpython/inithoc.cpp @@ -207,6 +207,43 @@ static int have_opt(const char* arg) { return 0; } +#if __linux__ + +/* we do this because thread sanitizer does not allow system calls. + In particular + system("stty sane") + returns an error code of 139 +*/ + +#include +#include +#include + +static void set_terminal_sane() { + struct termios settings; + + if (tcgetattr(STDIN_FILENO, &settings) != 0) { + std::cerr << "Error getting terminal attributes\r\n"; + return; + } + + // Set the specific attributes back to their sane values + settings.c_iflag |= (BRKINT | IMAXBEL); + settings.c_iflag &= ~(IGNBRK | INLCR | IXOFF | IUTF8); + + settings.c_oflag |= (OPOST | ONLCR); // ONLCR gives correct newlines + settings.c_oflag &= ~(ONLRET | OFILL | ONOCR); // Clear flags not needed + + settings.c_cflag |= (CS8); + + settings.c_lflag |= (ISIG | ICANON | IEXTEN | ECHO); + + if (tcsetattr(STDIN_FILENO, TCSANOW, &settings) != 0) { + std::cerr << "Error setting terminal attributes\r\n"; + } +} +#endif // __linux__ + void nrnpython_finalize() { #if NRN_ENABLE_THREADS if (main_thread_ == std::this_thread::get_id()) { @@ -215,9 +252,8 @@ void nrnpython_finalize() { #endif Py_Finalize(); } -#if linux - if (system("stty sane > /dev/null 2>&1")) { - } // 'if' to avoid ignoring return value warning +#if __linux__ + set_terminal_sane(); #endif }