From c62b30b4d0b30424d3a7481d76eded9bf338352e Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Fri, 9 Jan 2026 10:44:43 +0100 Subject: [PATCH] Improve robustness by catching unhandled app exceptions --- CHANGELOG.md | 1 + .../lib/mpos/activity_navigator.py | 5 ++- internal_filesystem/lib/mpos/ui/view.py | 35 +++++++++++++++---- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f34f12c5..05765e17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Simplify: don't rate-limit update_ui_threadsafe_if_foreground - 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 0.5.2 ===== diff --git a/internal_filesystem/lib/mpos/activity_navigator.py b/internal_filesystem/lib/mpos/activity_navigator.py index 58603759..7dccee3d 100644 --- a/internal_filesystem/lib/mpos/activity_navigator.py +++ b/internal_filesystem/lib/mpos/activity_navigator.py @@ -50,7 +50,10 @@ class ActivityNavigator: activity._result_callback = result_callback # Pass callback to activity start_time = utime.ticks_ms() mpos.ui.save_and_clear_current_focusgroup() - activity.onCreate() + try: + activity.onCreate() + except Exception as e: + print(f"activity.onCreate caught exception: {e}") end_time = utime.ticks_diff(utime.ticks_ms(), start_time) print(f"apps.py _launch_activity: activity.onCreate took {end_time}ms") return activity diff --git a/internal_filesystem/lib/mpos/ui/view.py b/internal_filesystem/lib/mpos/ui/view.py index 8315ca16..26069574 100644 --- a/internal_filesystem/lib/mpos/ui/view.py +++ b/internal_filesystem/lib/mpos/ui/view.py @@ -9,8 +9,14 @@ def setContentView(new_activity, new_screen): global screen_stack if screen_stack: current_activity, current_screen, current_focusgroup, _ = screen_stack[-1] - current_activity.onPause(current_screen) - current_activity.onStop(current_screen) + try: + current_activity.onPause(current_screen) + except Exception as e: + print(f"onPause caught exception: {e}") + try: + current_activity.onStop(current_screen) + except Exception as e: + print(f"onStop caught exception: {e}") from .util import close_top_layer_msgboxes close_top_layer_msgboxes() @@ -18,10 +24,16 @@ def setContentView(new_activity, new_screen): screen_stack.append((new_activity, new_screen, lv.group_create(), None)) if new_activity: - new_activity.onStart(new_screen) + try: + new_activity.onStart(new_screen) + except Exception as e: + print(f"onStart caught exception: {e}") lv.screen_load_anim(new_screen, lv.SCR_LOAD_ANIM.OVER_LEFT, 500, 0, False) if new_activity: - new_activity.onResume(new_screen) + try: + new_activity.onResume(new_screen) + except Exception as e: + print(f"onResume caught exception: {e}") def remove_and_stop_all_activities(): global screen_stack @@ -31,9 +43,18 @@ def remove_and_stop_all_activities(): def remove_and_stop_current_activity(): current_activity, current_screen, current_focusgroup, _ = screen_stack.pop() if current_activity: - current_activity.onPause(current_screen) - current_activity.onStop(current_screen) - current_activity.onDestroy(current_screen) + try: + current_activity.onPause(current_screen) + except Exception as e: + print(f"onPause caught exception: {e}") + try: + current_activity.onStop(current_screen) + except Exception as e: + print(f"onStop caught exception: {e}") + try: + current_activity.onDestroy(current_screen) + except Exception as e: + print(f"onDestroy caught exception: {e}") if current_screen: current_screen.clean()