Catching the IDE "stop" in uPython

How does the IDE stop the running uPython program on the camera? Does it send some sort of signal for which a handler could be registered? Or does it reset the camera in a way that is outside of uPython control or what?

It would be useful to be able to catch the stop.

UPDATE: I noticed the serial terminal echoing something like “Exception: IDE interrupt” … ?

It resets the scripts outside of uPython control. Anyway, we are about to do a release. Maybe Ibrahim can add something you need. Can you describe it?

The IDE sends a packet over USB/CDC to pend/raise an exception which interrupts uPython, and yes you can catch this exception and handle it, however the IDE won’t be able to stop the script if you do, additionally, the handler disables the USB FS interrupt before raising this exception, and it’s re-enabled before executing another script or REPL.