Skip to content

arch/sim: don't block on the wrapped RX segment of the simulated UART - #20028

Closed
raiden00pl wants to merge 2 commits into
apache:masterfrom
raiden00pl:fix-sim-uart-rx-wrap-block
Closed

arch/sim: don't block on the wrapped RX segment of the simulated UART#20028
raiden00pl wants to merge 2 commits into
apache:masterfrom
raiden00pl:fix-sim-uart-rx-wrap-block

Conversation

@raiden00pl

Copy link
Copy Markdown
Member

Summary

The console fd is blocking (host_uart_start() never sets O_NONBLOCK), so when a burst of input ends exactly at the wrap boundary of the RX circular buffer, the second host_uart_gets() in tty_dmareceive() blocks inside the timer callback and freezes the whole simulator until the host sends another byte. Poll the fd first so the wrapped segment is only read when data is really available.

Impact

we can remove workaround in NTFC for sim:

https://github.com/apache/nuttx-ntfc/blob/f68efe4e6080c0fa7018664cd284789d3314cf55/src/ntfc/device/sim.py#L38-L39

Testing

Simulator works on NTFC without workaround from above

The console fd is blocking (host_uart_start() never sets O_NONBLOCK), so
when a burst of input ends exactly at the wrap boundary of the RX
circular buffer, the second host_uart_gets() in tty_dmareceive() blocks
inside the timer callback and freezes the whole simulator until the host
sends another byte. Poll the fd first so the wrapped segment is only
read when data is really available.

Assisted-by: Claude Code
Signed-off-by: raiden00pl <raiden00@railab.me>
fix nxstyle errors

Signed-off-by: raiden00pl <raiden00@railab.me>
@github-actions github-actions Bot added Arch: simulator Issues related to the SIMulator Size: XS The size of the change in this PR is very small labels Sep 1, 2026
@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

xfer->nbytes = ret;

if (ret == xfer->length && xfer->nlength > 0)
/* The console fd is blocking, so only continue into the wrapped

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we change fd to the non-blocking mode

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changing to non-blocking requires rework of more logic, this is the minimal correct fix.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uart_nputs() won't work well with it. Simulated uarts never execute this path, sim console fd is always forced to host fd=0.

Another thing is that we will change default host fd 0 setting (we have to call fcntl(0, F_SETFL, O_NONBLOCK) ) which we should recover after nuttx simulator is closed or crash.

This fix is ​​a one-line fix, changing it to NONBLOCK is a rebuild of how the sim console works.

@xiaoxiang781216 xiaoxiang781216 Sep 1, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uart_nputs() won't work well with it. Simulated uarts never execute this path, sim console fd is always forced to host fd=0.

Let's add retry in uaart_nputs?

Another thing is that we will change default host fd 0 setting (we have to call fcntl(0, F_SETFL, O_NONBLOCK) )

yes, here is the change: #20032
we use internally more than half year without problem.

which we should recover after nuttx simulator is closed or crash.

why need? non-block is the property of file handle, not the terminal.

This fix is ​​a one-line fix, changing it to NONBLOCK is a rebuild of how the sim console works.

@raiden00pl

Copy link
Copy Markdown
Member Author

lets use #20032 which also fixes the problem

@raiden00pl raiden00pl closed this Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: simulator Issues related to the SIMulator Size: XS The size of the change in this PR is very small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants