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-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 72937e7f8 interpreters/duktape: Handle SIGINT gracefully to prevent 
crash on restart
72937e7f8 is described below

commit 72937e7f86f6effda78187c87c6da9ded0635be6
Author: Sarthak Pandey <[email protected]>
AuthorDate: Sat Oct 17 13:57:06 2026 +0530

    interpreters/duktape: Handle SIGINT gracefully to prevent crash on restart
    
    When Duktape is interrupted via Ctrl+C (SIGINT), the task was abruptly
    terminated by NuttX's default abnormal termination handler without
    destroying the Duktape heap. On flat-memory architectures such as ESP32,
    this leaked heap allocations and corrupted internal state, causing 
subsequent
    invocations of 'duk' to crash the board.
    
    This patch:
    1. Enables DUK_CMDLINE_SIGNAL conditionally in Makefile when
       CONFIG_ENABLE_ALL_SIGNALS=y to register a SIGINT handler.
    2. Defines DUK_USE_INTERRUPT_COUNTER and DUK_USE_EXEC_TIMEOUT_CHECK in
       duk_config.h to periodically inspect the interruption state.
    3. Implements an async-signal-safe interrupt handler and execution timeout
       check in duk_cmdline.c that stops bytecode execution cleanly, unwinds
       the call stack, and safely invokes destroy_duktape_heap() before exiting.
    4. Adds CMakeLists.txt to maintain build-system parity with CMake.
    
    Fixes #2855
    
    [skip-cmake-parity]
    Signed-off-by: Sarthak Pandey <[email protected]>
---
 interpreters/duktape/CMakeLists.txt    | 81 +++++++++++++++++++++++++++++
 interpreters/duktape/Makefile          |  3 ++
 interpreters/duktape/duk_cmdline.patch | 95 +++++++++++++++++++++++++++++++++-
 3 files changed, 177 insertions(+), 2 deletions(-)

diff --git a/interpreters/duktape/CMakeLists.txt 
b/interpreters/duktape/CMakeLists.txt
new file mode 100644
index 000000000..f3536896f
--- /dev/null
+++ b/interpreters/duktape/CMakeLists.txt
@@ -0,0 +1,81 @@
+# 
##############################################################################
+# apps/interpreters/duktape/CMakeLists.txt
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more 
contributor
+# license agreements.  See the NOTICE file distributed with this work for
+# additional information regarding copyright ownership.  The ASF licenses this
+# file to you under the Apache License, Version 2.0 (the "License"); you may 
not
+# use this file except in compliance with the License.  You may obtain a copy 
of
+# the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations under
+# the License.
+#
+# 
##############################################################################
+
+if(CONFIG_INTERPRETERS_DUKTAPE)
+  set(DUKTAPE_VERSION 2.5.0)
+  set(DUKTAPE_DIR ${CMAKE_CURRENT_LIST_DIR}/duktape)
+
+  if(NOT EXISTS ${DUKTAPE_DIR})
+    set(DUKTAPE_URL_BASE 
https://github.com/svaarala/duktape/releases/download/)
+
+    FetchContent_Declare(
+      duktape_fetch
+      URL 
${DUKTAPE_URL_BASE}/v${DUKTAPE_VERSION}/duktape-${DUKTAPE_VERSION}.tar.xz
+          SOURCE_DIR
+          ${CMAKE_CURRENT_LIST_DIR}/duktape
+          BINARY_DIR
+          ${NUTTX_BINARY_DIR}/apps/interpreters/duktape/duktape
+      PATCH_COMMAND patch -p0 -d ${CMAKE_CURRENT_LIST_DIR} <
+                    ${CMAKE_CURRENT_LIST_DIR}/duk_cmdline.patch
+      DOWNLOAD_NO_PROGRESS true
+      TIMEOUT 30)
+
+    FetchContent_GetProperties(duktape_fetch)
+
+    if(NOT duktape_fetch_POPULATED)
+      FetchContent_Populate(duktape_fetch)
+    endif()
+  endif()
+
+  set(DUKTAPE_FLAGS -Wno-unused-but-set-variable -DDUK_CMDLINE_CONSOLE_SUPPORT
+                    -DDUK_CMDLINE_PRINTALERT_SUPPORT)
+
+  if(CONFIG_ENABLE_ALL_SIGNALS)
+    list(APPEND DUKTAPE_FLAGS -DDUK_CMDLINE_SIGNAL)
+  endif()
+
+  set(DUKTAPE_INCDIR ${DUKTAPE_DIR}/src-noline ${DUKTAPE_DIR}/extras/console
+                     ${DUKTAPE_DIR}/extras/print-alert)
+
+  set(DUKTAPE_CSRCS
+      ${DUKTAPE_DIR}/src-noline/duktape.c
+      ${DUKTAPE_DIR}/extras/console/duk_console.c
+      ${DUKTAPE_DIR}/extras/print-alert/duk_print_alert.c)
+
+  nuttx_add_application(
+    MODULE
+    ${CONFIG_INTERPRETERS_DUKTAPE}
+    NAME
+    duk
+    STACKSIZE
+    ${CONFIG_INTERPRETERS_DUKTAPE_STACKSIZE}
+    PRIORITY
+    ${CONFIG_INTERPRETERS_DUKTAPE_PRIORITY}
+    SRCS
+    ${DUKTAPE_DIR}/examples/cmdline/duk_cmdline.c
+    ${DUKTAPE_CSRCS}
+    INCLUDE_DIRECTORIES
+    ${DUKTAPE_INCDIR}
+    COMPILE_FLAGS
+    ${DUKTAPE_FLAGS})
+
+endif()
diff --git a/interpreters/duktape/Makefile b/interpreters/duktape/Makefile
index c6a0a621e..f608d16a6 100644
--- a/interpreters/duktape/Makefile
+++ b/interpreters/duktape/Makefile
@@ -49,6 +49,9 @@ CFLAGS += -I$(DUKTAPE_UNPACK)/extras/console
 CFLAGS += -I$(DUKTAPE_UNPACK)/extras/print-alert
 CFLAGS += -DDUK_CMDLINE_CONSOLE_SUPPORT
 CFLAGS += -DDUK_CMDLINE_PRINTALERT_SUPPORT
+ifeq ($(CONFIG_ENABLE_ALL_SIGNALS),y)
+CFLAGS += -DDUK_CMDLINE_SIGNAL
+endif
 CFLAGS += -Wno-unused-but-set-variable
 
 $(DUKTAPE_TARBALL):
diff --git a/interpreters/duktape/duk_cmdline.patch 
b/interpreters/duktape/duk_cmdline.patch
index e8a707895..848b1531d 100644
--- a/interpreters/duktape/duk_cmdline.patch
+++ b/interpreters/duktape/duk_cmdline.patch
@@ -2,6 +2,7 @@ diff --color -ur duktape/examples/cmdline/duk_cmdline.c 
duktape-2.5.0-modifed/ex
 --- duktape/examples/cmdline/duk_cmdline.c     2019-11-25 06:04:27.000000000 
+0800
 +++ duktape-2.5.0-modifed/examples/cmdline/duk_cmdline.c       2020-08-07 
23:20:27.320000000 +0800
 @@ -103,7 +103,7 @@
+ 
  #define  MEM_LIMIT_NORMAL   (128*1024*1024)   /* 128 MB */
  #define  MEM_LIMIT_HIGH     (2047*1024*1024)  /* ~2 GB */
 -#define  LINEBUF_SIZE       65536
@@ -9,10 +10,47 @@ diff --color -ur duktape/examples/cmdline/duk_cmdline.c 
duktape-2.5.0-modifed/ex
  
  static int main_argc = 0;
  static char **main_argv = NULL;
-
-@@ -810,10 +810,12 @@
+@@ -167,14 +167,28 @@ static void set_resource_limits(rlim_t m
+ #endif  /* DUK_CMDLINE_RLIMIT */
+ 
+ #if defined(DUK_CMDLINE_SIGNAL)
++#include <signal.h>
++
++static volatile sig_atomic_t g_interrupted = 0;
++
++duk_bool_t duk_cmdline_timeout_check(void *udata) {
++      (void) udata;
++      return g_interrupted;
++}
++
+ static void my_sighandler(int x) {
+-      fprintf(stderr, "Got signal %d\n", x);
+-      fflush(stderr);
++      (void) x;
++      g_interrupted = 1;
+ }
+ static void set_sigint_handler(void) {
+       (void) signal(SIGINT, my_sighandler);
+       (void) signal(SIGPIPE, SIG_IGN);  /* avoid SIGPIPE killing process */
+ }
++#else
++duk_bool_t duk_cmdline_timeout_check(void *udata) {
++      (void) udata;
++      return 0;
++}
+ #endif  /* DUK_CMDLINE_SIGNAL */
+ 
+ static void cmdline_fatal_handler(void *udata, const char *msg) {
+@@ -809,11 +823,19 @@ static int handle_interactive(duk_contex
+               fflush(stdout);
  
                for (;;) {
++#if defined(DUK_CMDLINE_SIGNAL)
++                      if (g_interrupted) {
++                              got_eof = 1;
++                              break;
++                      }
++#endif
                        int c = fgetc(stdin);
 +                      fputc(c, stdout);
 +                      fflush(stdout);
@@ -24,6 +62,29 @@ diff --color -ur duktape/examples/cmdline/duk_cmdline.c 
duktape-2.5.0-modifed/ex
                                break;
                        } else if (idx >= LINEBUF_SIZE) {
                                fprintf(stderr, "line too long\n");
+@@ -1268,6 +1290,9 @@ int main(int argc, char *argv[]) {
+ 
+       main_argc = argc;
+       main_argv = (char **) argv;
++#if defined(DUK_CMDLINE_SIGNAL)
++      g_interrupted = 0;
++#endif
+ 
+ #if defined(EMSCRIPTEN)
+       /* Try to use NODEFS to provide access to local files.  Mount the
+@@ -1509,6 +1534,12 @@ int main(int argc, char *argv[]) {
+               fflush(stderr);
+       }
+ 
++#if defined(DUK_CMDLINE_SIGNAL)
++      if (g_interrupted) {
++              retval = 130;
++      }
++#endif
++
+       if (ctx && no_heap_destroy) {
+               duk_gc(ctx, 0);
+       }
 
 diff --color -ur duktape/src-noline/duk_config.h 
duktape-2.5.0-modifed/src-noline/duk_config.h
 --- duktape/src-noline/duk_config.h    2019-11-25 06:04:27.000000000 +0800
@@ -44,3 +105,33 @@ diff --color -ur duktape/src-noline/duk_config.h 
duktape-2.5.0-modifed/src-nolin
 -#define DUK_USE_DATE_PRS_STRPTIME
 +#undef DUK_USE_DATE_PRS_STRPTIME
  #define DUK_USE_DATE_FMT_STRFTIME
+ 
+ #if 0  /* XXX: safe condition? */
+@@ -2962,7 +2962,17 @@ typedef struct duk_hthread duk_context;
+ #undef DUK_USE_EXEC_INDIRECT_BOUND_CHECK
+ #undef DUK_USE_EXEC_PREFER_SIZE
+ #define DUK_USE_EXEC_REGCONST_OPTIMIZE
+-#undef DUK_USE_EXEC_TIMEOUT_CHECK
++#define DUK_USE_EXEC_TIMEOUT_CHECK(udata) duk_cmdline_timeout_check(udata)
++#if !defined(DUK_CMDLINE_TIMEOUT_CHECK_DECLARED)
++#define DUK_CMDLINE_TIMEOUT_CHECK_DECLARED
++#ifdef __cplusplus
++extern "C" {
++#endif
++duk_bool_t duk_cmdline_timeout_check(void *udata);
++#ifdef __cplusplus
++}
++#endif
++#endif
+ #undef DUK_USE_EXPLICIT_NULL_INIT
+ #undef DUK_USE_EXTSTR_FREE
+ #undef DUK_USE_EXTSTR_INTERN_CHECK
+@@ -3004,7 +3014,7 @@ typedef struct duk_hthread duk_context;
+ #define DUK_USE_HTML_COMMENTS
+ #define DUK_USE_IDCHAR_FASTPATH
+ #undef DUK_USE_INJECT_HEAP_ALLOC_ERROR
+-#undef DUK_USE_INTERRUPT_COUNTER
++#define DUK_USE_INTERRUPT_COUNTER
+ #undef DUK_USE_INTERRUPT_DEBUG_FIXUP
+ #define DUK_USE_JC
+ #define DUK_USE_JSON_BUILTIN

Reply via email to