Replies: 8 comments 5 replies
-
Try this: import sys
def test():
print('In test')
sys.exit(42)
print('Out test')
try:
print('before exit')
w = test()
except SystemExit as e:
print('SystemExit', e)
finally:
print('after exit') |
Beta Was this translation helpful? Give feedback.
-
It's very depending of your board.. try to be more precise... Sharit suggestion is ok but for some boards it don't work.. |
Beta Was this translation helpful? Give feedback.
-
I should have included that. Espressif ESP32-S3-DevKitC-1.1, N8 variant, firmware v1.24.1 (latest). It does a soft reset, which I don’t want. I want it to stop execution and go the REPL like prior firmware did.
I tried your earlier suggestion using exit(42) but that just did a soft reboot too.
Right now, I have a kludge workaround. Instead of sys.exit() I substituted a known-to-be-junk instruction sys.exit_to_repl() that simply causes the script to crash. But that’s not, to put it mildly, very elegant. Nor, I doubt, very pythonic.
From: shariltumin ***@***.***>
Sent: Wednesday, January 1, 2025 11:20 AM
To: micropython/micropython ***@***.***>
Cc: smhodge42 ***@***.***>; Author ***@***.***>
Subject: Re: [micropython/micropython] sys.exit() causes soft reboot (Discussion #16509)
May I ask on which board and with which version of MP the code did not work? If it did not work, what happened? Did it fall back to REPL or did a soft reset?
—
Reply to this email directly, view it on GitHub<#16509 (reply in thread)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/A2XPQKTVBU2F4QSSIU4EHT32IQ5WRAVCNFSM6AAAAABUNZD5EOVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTCNZRGE4DMOA>.
You are receiving this because you authored the thread.Message ID: ***@***.******@***.***>>
|
Beta Was this translation helpful? Give feedback.
-
You can always raise a different exception is sys.exit() does not works as expected. But the script example above by @shariltumin works fine with an ESP32S3 here. And there is nothing specific for a certain ESP32 variant in it. If it causes a reboot, there may be another problem with your set-up. |
Beta Was this translation helpful? Give feedback.
-
@smhodge42, you might want to try MicroPython v1.25.0-preview.xxx.yyy
You can use any number you like in |
Beta Was this translation helpful? Give feedback.
-
Yes, I can get that script to work just fine in the REPL, but when I put it into my code, it just does the same soft reboot.
From: shariltumin ***@***.***>
Sent: Wednesday, January 1, 2025 12:42 PM
To: micropython/micropython ***@***.***>
Cc: smhodge42 ***@***.***>; Mention ***@***.***>
Subject: Re: [micropython/micropython] sys.exit() causes soft reboot (Discussion #16509)
@smhodge42<https://github.com/smhodge42>, you might want to try MicroPython v1.25.0-preview.xxx.yyy
$ mpremote mount .
Local directory . is mounted at /remote
Connected to MicroPython at /dev/ttyACM0
Use Ctrl-] or Ctrl-x to exit this shell
MicroPython v1.25.0-preview.6.g548babf8a on 2024-10-30; Generic ESP32S3 module w
ith ESP32S3
Type "help()" for more information.
>> import test_exit
before exit
In test
SystemExit 42
after exit
>>
You can use any number you like in sys.exit() 42 was meant as a joke.
-
Reply to this email directly, view it on GitHub<#16509 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/A2XPQKQE46SXPBLVVBKM2BD2IRHH3AVCNFSM6AAAAABUNZD5EOVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTCNZRGIZDAOA>.
You are receiving this because you were mentioned.Message ID: ***@***.******@***.***>>
|
Beta Was this translation helpful? Give feedback.
-
Here is my code. It is a method in a class:
def stopCos(self, err):
import sys
print("Fatal error", err)
try:
print('attempt to exit')
sys.exit(1)
except Exception as e:
print('Other exception', e)
except SystemExit as e:
print ('SystemExit', e)
finally:
print ('no exit, continue execution')
Here is the output when it gets triggered. It is passed err=12.
Fatal error 12
attempt to exit
SystemExit 1
no exit, continue execution
...execution does not stop but continues until it bombs because of the condition that triggered the error. In the old firmware, the sys.exit(1) would return to the REPL. If I don’t catch the exception, it does a soft reboot.
From: shariltumin ***@***.***>
Sent: Wednesday, January 1, 2025 2:14 PM
To: micropython/micropython ***@***.***>
Cc: smhodge42 ***@***.***>; Mention ***@***.***>
Subject: Re: [micropython/micropython] sys.exit() causes soft reboot (Discussion #16509)
You can try these try..except blocks in you code.
try:
w = your_prog()
except:
print('SystemExit maybe?')
finally:
print('Cleanup!')
or
try:
w = your_prog()
except Exception as e:
print('Other exception', e)
except SystemExit as e:
print('SystemExit', e)
finally:
print('Cleanup!')
—
Reply to this email directly, view it on GitHub<#16509 (reply in thread)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/A2XPQKWSVXL2J72SEZENL7L2IRSEFAVCNFSM6AAAAABUNZD5EOVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTCNZRGI2TSMA>.
You are receiving this because you were mentioned.Message ID: ***@***.******@***.***>>
|
Beta Was this translation helpful? Give feedback.
-
My main.py is basically a setup+loop, like Arduino:
import startup # All startup tasks in here, including the one that is the source of the fatal error
from commands import cmd
while (True):
cmd.getPacket() # Process packets that arrive at ports
cmd.getEvent() # Process interrupt events, most of them triggered by an external RTC
startup.py imports many other modules, including <log.py> which is the source of the fatal error, and which calls stopCos(), which is in yet another module <cos_system.py>.
It’s all very complex and confusing but the long and the short of this is that it would have been a pretty tedious task to restructure as you suggested, one that had no guarantee of success. Instead, I got an ok result by wrapping the initial “import startup” into the try…except:
try:
import startup
except Exception as e:
print('Other exception', e)
except SystemExit as e:
print('SystemExit', e)
raise Exception("COS stopped")
and with stopCos() now simply:
def stopCos(err):
import sys
sys.exit(err)
The startup output, with the error triggered, is now:
…
******** SD error, most likely no card present
Stop (s), restart (r) or continue with SD logging disabled (d)? # User selects ‘s’, get…
SystemExit 12
Traceback (most recent call last):
File "main.py", line 7, in <module>
Exception: COS stopped
MicroPython v1.24.1 on 2024-11-29; Generic ESP32S3 module with ESP32S3
Type "help()" for more information.
>>
At any rate it’s back to the REPL and no soft reboot in a somewhat more elegant way, which is ok with me.
Thanks for your help.
From: shariltumin ***@***.***>
Sent: Thursday, January 2, 2025 4:32 AM
To: micropython/micropython ***@***.***>
Cc: smhodge42 ***@***.***>; Mention ***@***.***>
Subject: Re: [micropython/micropython] sys.exit() causes soft reboot (Discussion #16509)
I see what might be causing the problem you mention. You are handling the SystemExit exception locally, i.e. within your code block itself, so the stopCos() is not terminating your main execution loop. You may want to restructure your script.
I assume you have your main class as Prog and a method called go() that executes your main loop. For example, you might want to do this:
import sys
class Prog():
def __init__(self):
...
def cleanup():
...
def stopCos(self, err):
print("Fatal error", err)
self.cleanup() # do cleanup, gc.collect() ...
sys.exit(err)
...
def go(self):
while True:
...
...
if Err: self.stopCos(self.FatalErrCode)
prog = Prog()
try:
prog.go()
except Exception as e:
print('Other exception', e)
except SystemExit as e:
print('SystemExit', e)
finally:
print('Cleanup!')
—
Reply to this email directly, view it on GitHub<#16509 (reply in thread)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/A2XPQKR6FHJGEW4RWL3PRXL2IUWTTAVCNFSM6AAAAABUNZD5EOVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTCNZRGY2TQMY>.
You are receiving this because you were mentioned.Message ID: ***@***.******@***.***>>
|
Beta Was this translation helpful? Give feedback.
-
In prior firmware (forget which version), sys.exit(1) would truly stop execution of my code, IIRC returning to the REPL prompt, which was exactly what I wanted. Now the upgraded firmware does a soft reboot, which in many circumstances in my code just causes things to repeat, sometimes endlessly, which is definitely not what I want.
Is there a workaround or a (now) correct technique to stop execution without doing a reboot? The docs say the above action occurs for an unhandled SystemExit, but I can't figure out how to "handle it" to get back to the same action as the earlier firmware.
Beta Was this translation helpful? Give feedback.
All reactions