From 304fa1a53768993b15f8e2bb56202e3a7121e41a Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Fri, 9 Jan 2026 12:03:33 +0100 Subject: [PATCH] Improve robustness with custom exception that does not deinit() the TaskHandler --- CHANGELOG.md | 2 ++ internal_filesystem/lib/mpos/main.py | 23 +++++++++++++++-------- lvgl_micropython | 2 +- scripts/build_mpos.sh | 4 ++-- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05765e17..d8fe48ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ - WiFi app: check "hidden" in EditNetwork - Wifi app: add support for scanning wifi QR codes to "Add Network" - Improve robustness by catching unhandled app exceptions +- Improve robustness with custom exception that does not deinit() the TaskHandler +- Improve robustness by removing TaskHandler callback that throws an uncaught exception 0.5.2 ===== diff --git a/internal_filesystem/lib/mpos/main.py b/internal_filesystem/lib/mpos/main.py index e576195a..88caabcd 100644 --- a/internal_filesystem/lib/mpos/main.py +++ b/internal_filesystem/lib/mpos/main.py @@ -51,20 +51,27 @@ focusgroup = lv.group_get_default() if focusgroup: # on esp32 this may not be set focusgroup.remove_all_objs() # might be better to save and restore the group for "back" actions -# Can be passed to TaskHandler, currently unused: +# Custom exception handler that does not deinit() the TaskHandler because then the UI hangs: def custom_exception_handler(e): - print(f"custom_exception_handler called: {e}") - mpos.ui.task_handler.deinit() + print(f"TaskHandler's custom_exception_handler called: {e}") + import sys + sys.print_exception(e) # NOQA + # No need to deinit() and re-init LVGL: + #mpos.ui.task_handler.deinit() # default task handler does this, but then things hang # otherwise it does focus_next and then crashes while doing lv.deinit() - focusgroup.remove_all_objs() - focusgroup.delete() - lv.deinit() + #focusgroup.remove_all_objs() + #focusgroup.delete() + #lv.deinit() import sys if sys.platform == "esp32": - mpos.ui.task_handler = task_handler.TaskHandler(duration=5) # 1ms gives highest framerate on esp32-s3's but might have side effects? + mpos.ui.task_handler = task_handler.TaskHandler(duration=5, exception_hook=custom_exception_handler) # 1ms gives highest framerate on esp32-s3's but might have side effects? else: - mpos.ui.task_handler = task_handler.TaskHandler(duration=5) # 5ms is recommended for MicroPython+LVGL on desktop (less results in lower framerate) + mpos.ui.task_handler = task_handler.TaskHandler(duration=5, exception_hook=custom_exception_handler) # 5ms is recommended for MicroPython+LVGL on desktop (less results in lower framerate) + +# Convenient for apps to be able to access these: +mpos.ui.task_handler.TASK_HANDLER_STARTED = task_handler.TASK_HANDLER_STARTED +mpos.ui.task_handler.TASK_HANDLER_FINISHED = task_handler.TASK_HANDLER_FINISHED try: import freezefs_mount_builtin diff --git a/lvgl_micropython b/lvgl_micropython index 6a140929..900c8929 160000 --- a/lvgl_micropython +++ b/lvgl_micropython @@ -1 +1 @@ -Subproject commit 6a1409298593c557b724f3de6c2d0c7504c881d0 +Subproject commit 900c89296d7a6077f7532c6523e405ff06b7c3cd diff --git a/scripts/build_mpos.sh b/scripts/build_mpos.sh index 6bd1db2a..506089f3 100755 --- a/scripts/build_mpos.sh +++ b/scripts/build_mpos.sh @@ -143,8 +143,8 @@ elif [ "$target" == "unix" -o "$target" == "macOS" ]; then # Restore RLOTTIE: if [ "$target" == "unix" ]; then sed -i.backup 's/#define MICROPY_RLOTTIE 1/#define MICROPY_RLOTTIE 0/' "$codebasedir"/lvgl_micropython/lib/lv_conf.h - echo "After disabling MICROPY_RLOTTIE:" - cat "$codebasedir"/lvgl_micropython/lib/lv_conf.h + #echo "After disabling MICROPY_RLOTTIE:" + #cat "$codebasedir"/lvgl_micropython/lib/lv_conf.h fi # Restore @micropython.viper decorator after build