From 256e5bfd2dbda3bdd3b2c865d24737258a7e0072 Mon Sep 17 00:00:00 2001 From: chenzihan1 Date: Wed, 20 Aug 2025 22:13:30 +0800 Subject: [PATCH 1/3] sim_hostuart.c: add nonblock flags for /dev/console to avoid the blocking indefinitely when the host uart is not ready to read/write data. Signed-off-by: chenzihan1 --- arch/sim/src/sim/posix/sim_hostuart.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arch/sim/src/sim/posix/sim_hostuart.c b/arch/sim/src/sim/posix/sim_hostuart.c index 04254bc882848..4976aadd902ad 100644 --- a/arch/sim/src/sim/posix/sim_hostuart.c +++ b/arch/sim/src/sim/posix/sim_hostuart.c @@ -95,6 +95,8 @@ static void restoremode(void) void host_uart_start(void) { + int flags; + /* Get the current stdin terminal mode */ tcgetattr(0, &g_cooked); @@ -103,6 +105,12 @@ void host_uart_start(void) setrawmode(0); + flags = host_uninterruptible(fcntl, 0, F_GETFL, 0); + if (flags > 0) + { + host_uninterruptible_no_return(fcntl, 0, F_SETFL, flags | O_NONBLOCK); + } + /* Restore the original terminal mode before exit */ atexit(restoremode); From 576a8a8a8be26aee6dbf6224a68c6da8a4544447 Mon Sep 17 00:00:00 2001 From: ligd Date: Sat, 28 Feb 2026 12:11:14 +0800 Subject: [PATCH 2/3] sim/hostuart: set stdout to O_NONBLOCK to prevent blocking When NuttX sim is connected via pipe (e.g. by an automation tool), if the host side does not read stdout in time, the pipe buffer fills up and write(1, ...) blocks the entire sim process. This happens inside host_uninterruptible (irq disabled), so the NuttX scheduler is completely frozen and all threads stop. Set fd=1 (stdout) to O_NONBLOCK so that write() returns EAGAIN instead of blocking. The TX data stays in the NuttX xmit buffer and sim_tty_work retries every 1ms until the pipe has space. Signed-off-by: ligd --- arch/sim/src/sim/posix/sim_hostuart.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/sim/src/sim/posix/sim_hostuart.c b/arch/sim/src/sim/posix/sim_hostuart.c index 4976aadd902ad..d158e9f794c5b 100644 --- a/arch/sim/src/sim/posix/sim_hostuart.c +++ b/arch/sim/src/sim/posix/sim_hostuart.c @@ -111,6 +111,16 @@ void host_uart_start(void) host_uninterruptible_no_return(fcntl, 0, F_SETFL, flags | O_NONBLOCK); } + /* Set stdout to non-blocking to prevent write(1, ...) from blocking + * the entire sim process when the host pipe buffer is full. + */ + + flags = host_uninterruptible(fcntl, 1, F_GETFL, 0); + if (flags > 0) + { + host_uninterruptible_no_return(fcntl, 1, F_SETFL, flags | O_NONBLOCK); + } + /* Restore the original terminal mode before exit */ atexit(restoremode); From e94fe227ce254f1d90eccf9b6334a53a1fed0fba Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Tue, 1 Sep 2026 19:47:13 +0800 Subject: [PATCH 3/3] arch/sim: switch nonblock setup to ioctl FIONBIO Update host_uart_start() to set stdin/stdout nonblocking via ioctl. Signed-off-by: Xiang Xiao --- arch/sim/src/sim/posix/sim_hostuart.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/arch/sim/src/sim/posix/sim_hostuart.c b/arch/sim/src/sim/posix/sim_hostuart.c index d158e9f794c5b..a98410393ada0 100644 --- a/arch/sim/src/sim/posix/sim_hostuart.c +++ b/arch/sim/src/sim/posix/sim_hostuart.c @@ -95,7 +95,7 @@ static void restoremode(void) void host_uart_start(void) { - int flags; + int nonblock = 1; /* Get the current stdin terminal mode */ @@ -105,21 +105,13 @@ void host_uart_start(void) setrawmode(0); - flags = host_uninterruptible(fcntl, 0, F_GETFL, 0); - if (flags > 0) - { - host_uninterruptible_no_return(fcntl, 0, F_SETFL, flags | O_NONBLOCK); - } + host_uninterruptible_no_return(ioctl, 0, FIONBIO, &nonblock); /* Set stdout to non-blocking to prevent write(1, ...) from blocking * the entire sim process when the host pipe buffer is full. */ - flags = host_uninterruptible(fcntl, 1, F_GETFL, 0); - if (flags > 0) - { - host_uninterruptible_no_return(fcntl, 1, F_SETFL, flags | O_NONBLOCK); - } + host_uninterruptible_no_return(ioctl, 1, FIONBIO, &nonblock); /* Restore the original terminal mode before exit */