This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit a3c2083e43257e1e9637cd1fcec48f7d7e620420
Author: dengwenqi <[email protected]>
AuthorDate: Sun May 3 23:06:25 2026 +0800

    sched/assert: spin after board_reset() to prevent recursive assert
    
    reset_board() is documented to never return, but the
    CONFIG_BOARD_RESET_ON_ASSERT >= 1 branch simply calls board_reset() and
    falls through.  On platforms where board_reset() is asynchronous (for
    example qemu-armv7r writes to a PL061 GPIO to request an external reset
    and returns immediately), reset_board() returns back into _assert(),
    which then returns to its caller while g_nx_initstate is already
    OSINIT_PANIC.
    
    The code that asserted then keeps running until the reset takes
    effect, and the next fault re-enters _assert() and takes the
    OSINIT_IS_PANIC() fast path.
    
    Add an unconditional spin loop after the board_reset() call so that
    reset_board() honors the "never return" contract on every platform.
    The #else branch already contains its own infinite LED-flash loop, so
    the new spin is unreachable in that configuration and does not change
    existing behavior.
    
    Signed-off-by: dengwenqi <[email protected]>
---
 sched/misc/assert.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/sched/misc/assert.c b/sched/misc/assert.c
index 89378c7d241..15086315ce6 100644
--- a/sched/misc/assert.c
+++ b/sched/misc/assert.c
@@ -870,7 +870,17 @@ static void reset_board(void)
 #if CONFIG_BOARD_RESET_ON_ASSERT >= 1
   up_flush_dcache_all();
   board_reset(CONFIG_BOARD_ASSERT_RESET_VALUE);
-#else
+
+  /* board_reset() is permitted to be asynchronous (e.g. it just requests an
+   * external reset via a GPIO pin and returns immediately).  Per the
+   * function contract documented above ("It should never return."), spin
+   * here so that we do not fall back into the caller while g_nx_initstate
+   * is already OSINIT_PANIC.  If we did return, any subsequent fault would
+   * re-enter _assert(), hit the recursive-assert fast path, and mask the
+   * original crash context.
+   */
+#endif
+
   for (; ; )
     {
 #ifdef CONFIG_ARCH_LEDS
@@ -882,7 +892,6 @@ static void reset_board(void)
 #endif
       up_mdelay(250);
     }
-#endif
 }
 
 /****************************************************************************

Reply via email to