From 72caf6799cc69fa45af688bab1e94d61fb1b965c Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Tue, 2 Dec 2025 20:58:59 +0100 Subject: [PATCH] API: restore sys.path after starting app --- internal_filesystem/lib/mpos/apps.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/internal_filesystem/lib/mpos/apps.py b/internal_filesystem/lib/mpos/apps.py index 366d914e..a66102ec 100644 --- a/internal_filesystem/lib/mpos/apps.py +++ b/internal_filesystem/lib/mpos/apps.py @@ -37,7 +37,7 @@ def execute_script(script_source, is_file, cwd=None, classname=None): } print(f"Thread {thread_id}: starting script") import sys - path_before = sys.path + path_before = sys.path[:] # Make a copy, not a reference if cwd: sys.path.append(cwd) try: @@ -74,8 +74,10 @@ def execute_script(script_source, is_file, cwd=None, classname=None): tb = getattr(e, '__traceback__', None) traceback.print_exception(type(e), e, tb) return False - print(f"Thread {thread_id}: script {compile_name} finished, restoring sys.path to {sys.path}") - sys.path = path_before + finally: + # Always restore sys.path, even if we return early or raise an exception + print(f"Thread {thread_id}: script {compile_name} finished, restoring sys.path from {sys.path} to {path_before}") + sys.path = path_before return True except Exception as e: print(f"Thread {thread_id}: error:")