Recently my SX260HS froze twice with open lens soon after running a motion detection script*. This wasn't funny but I guess there's no damage in this.
This usually means the camera crashed. Sometimes the ROMLOG
https://chdk.fandom.com/wiki/Debugging#Camera_crash_logs_.28romlog.29 has hints about why, but I wouldn't bother getting one for a crash that happened using CHDK 1.2.
When you talk about "modifying the onboard flash" what can cause this?
I meant the flash ROM memory where the firmware and settings are stored. As I said, there is essentially no chance of doing this by accident, you'd need to intentionally call firmware functions with call_func_ptr or call_event_proc to do that.
Is it imperative also to take sleep() breaks after every click or when the camera is busy?
No. You should use sleep if you are waiting for something to happen, like waiting for the camera to be ready after half press.
In some cases, you might need additional waits, for example, maybe between switching to rec mode and then setting the capture mode. There is no hard and fast rule, it's something you can try if your script crashes the camera or if the some script actions seem to be ignored.
I saw the get_mode function but what about busy time while writing movies to the SD card?
get_video_recording and get_movie_status can tell you something about whether movies are recording. This is a case where you may need a bit of additional wait after the status goes to not recording. How long you have to wait (if at all) would need to based on testing.
In case you haven't already found it,
https://chdk.fandom.com/wiki/CHDK_Scripting_Cross_Reference_Page is a good place to start for script functions.