Skip to content

examples/rust: Avoid the infinite loop on sim for hello_rust_cargo#3544

Open
toku-mac wants to merge 1 commit into
apache:masterfrom
toku-mac:avoid_infinite_loop_on_sim
Open

examples/rust: Avoid the infinite loop on sim for hello_rust_cargo#3544
toku-mac wants to merge 1 commit into
apache:masterfrom
toku-mac:avoid_infinite_loop_on_sim

Conversation

@toku-mac

Copy link
Copy Markdown
Contributor

Summary

On the sim target, hello_rust_cargo_main() finished with an infinite loop {}.
As a result the example never returned to NSH and the prompt appeared to hang after running hello_rust_cargo.

This PR makes the example return cleanly to NSH on sim (Windows/macOS/Linux) while preserving the original looping behavior on real targets.

  • Add a Cargo sim feature to the hello crate.
  • Enable the feature only when CONFIG_ARCH_SIM is set, from both the CMake and Make build paths.
  • In lib.rs, compile out the trailing loop {} when the sim feature is active and return 0 instead.
  • Plumb a generic features mechanism through the Rust build helpers so the feature can be passed to cargo build consistently.

Testing

I confirm that changes are verified on local setup and works as intended:

  • Build Host(s): OS (macOS 26.5), CPU(Apple M1), compiler(Apple clang version 21.0.0)
  • Target(s): arch(sim)
  • Ensure your PATH environment variable is properly configured to allow execution of: menuconfig, olddefconfig, savedefconfig, and setconfig.
  • Use the Rust toolchain version prior to nightly-2026-04-29 to avoid errors related to lib/rustlib/src/rust/library/std/src/sys/net/connection/socket/unix.rs.

Configuration and Build:

with make

make distclean
./tools/configure.sh sim:nsh

printf 'CONFIG_SYSTEM_TIME64=y
CONFIG_FS_LARGEFILE=y
CONFIG_TLS_NELEM=16
CONFIG_DEV_URANDOM=y
CONFIG_EXAMPLES_HELLO_RUST_CARGO=y
CONFIG_EXAMPLES_HELLO_RUST_CARGO_STACKSIZE=8192
' >> .config

make olddefconfig
make

...
   Compiling hello v0.1.0 (/Users/toku/nuttxspace/apps/examples/rust/hello)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 29.52s
LN: platform/board to /Users/toku/nuttxspace/apps/platform/dummy
Register: gpio
Register: hello
Register: hello_rust_cargo
Register: dd
Register: dumpstack
Register: nsh
Register: sh
Register: ostest
CP:  /Users/toku/nuttxspace/nuttx/include/nuttx/config.h
CP:  /Users/toku/nuttxspace/nuttx/include/nuttx/fs/hostfs.h
Building Rust code with cargo...
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.78s
LD:  nuttx
ld: warning: -ld_classic is deprecated and will be removed in a future release
ld: warning: -ld_classic is deprecated and will be removed in a future release

with CMake

rm -rf build-debug
make distclean
cmake -S . -B build-debug -DBOARD_CONFIG=sim:nsh -GNinja

printf 'CONFIG_SYSTEM_TIME64=y
CONFIG_FS_LARGEFILE=y
CONFIG_TLS_NELEM=16
CONFIG_DEV_URANDOM=y
CONFIG_EXAMPLES_HELLO_RUST_CARGO=y
CONFIG_EXAMPLES_HELLO_RUST_CARGO_STACKSIZE=8192
' >> build-debug/.config

cmake --build build-debug -t olddefconfig
cmake --build build-debug

...
   Compiling pin-project-lite v0.2.17
   Compiling memchr v2.8.0
   Compiling itoa v1.0.18
   Compiling tokio v1.52.3
   Compiling hello v0.1.0 (/Users/toku/nuttxspace/apps/examples/rust/hello)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 35.66s
[1225/1230] Linking C executable nuttx
ld: warning: -ld_classic is deprecated and will be removed in a future release
[1227/1230] Patching Mach-O init section type flags

Execute:

NuttShell (NSH) NuttX-12.13.0
nsh> help
help usage:  help [-v] [<cmd>]

    .           cmp         false       mkfifo      readlink    time
    [           dirname     fdinfo      mkrd        rm          true
    ?           date        free        mount       rmdir       truncate
    alias       df          help        mv          set         uname
    unalias     dmesg       hexdump     pidof       kill        umount
    basename    echo        losetup     poweroff    pkill       unset
    break       env         ln          quit        sleep       uptime
    cat         exec        ls          printf      usleep      watch
    cd          exit        mkdir       ps          source      xd
    cp          expr        mkfatfs     pwd         test        wait

Builtin Apps:
    dd                  nsh                 ostest              hello
    dumpstack           sh                  gpio                hello_rust_cargo
nsh> uname -a
NuttX 12.13.0 82fb723bde-dirty Jun 16 2026 10:16:16 sim sim
nsh> hello_rust_cargo
{"name":"John","age":30}
{"name":"Jane","age":25}
Deserialized: Alice is 28 years old
Pretty JSON:
{
  "name": "Alice",
  "age": 28
}
Hello world from tokio!
nsh> poweroff

PR verification Self-Check

  • My PR adheres to Contributing Guidelines and Documentation (git commit title and message, coding standard, etc).
  • My PR is ready for review and can be safely merged into a codebase.

On the sim target (Windows/macOS/Linux), hello_rust_cargo_main() ended
with an infinite `loop {}` that never returned control to NSH, so the
example could not exit and the shell prompt hung.

Introduce a Cargo `sim` feature that is enabled only when CONFIG_ARCH_SIM
is set, and use it to compile out the trailing infinite loop and return 0
instead. Other embedded targets keep the original looping behavior.

Signed-off-by: Shoji Tokunaga <toku@mac.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants